【问题标题】:Python tkinter classes changing iconPython tkinter 类更改图标
【发布时间】:2017-04-02 13:19:46
【问题描述】:

我对课程或 tkinter 不太熟悉,但我想做的是,如果 选择了test1,而不是将图标更改为favicon,我真的无法弄清楚,如果你能帮助我,那就太好了,如果我做错了什么

from tkinter import *


class GUI:
    def __init__(self, master):

        self.iconnum = IntVar()
        master.title('Testing')
        master.resizable(width=False, height=False)
        master.maxsize(500, 250)
        master.minsize(500, 250)

        self.test1= Radiobutton(master, text="test1", variable=0, value=1, )
        self.test2= Radiobutton(master, text="test2", variable=0, value=2, )
        self.test3= Radiobutton(master, text="test3", variable=0, value=3, )
        self.test4= Radiobutton(master, text="test4", variable=0, value=4,  )

        self.test1.grid(row=2, columnspan=1)
        self.test2.grid(row=2, columnspan=2)
        self.test3.grid(row=2, column=1)
        self.test4.grid(row=3, columnspan=1,)

        self.Test5= Radiobutton(master, text="Test5", indicatoron=0, height=1, width=35,  value=0, command=self.icon_switcher)
        self.Test6= Radiobutton(master, text="Test6", indicatoron=0, height=1, width=35, value=1, command=self.icon_switcher)

        self.Test5.grid(row=1)
        self.Test6.grid(row=1, column=1,)

    def icon_switcher(self):
        if self.iconnum == 1:
            self.master.iconbitmap('favicon.ico')




root = Tk()
gui = GUI(root)

root.mainloop()

【问题讨论】:

    标签: python python-2.7 python-3.x class tkinter


    【解决方案1】:

    你需要给一个 tkinter IntVarfor RadioButton's variable 关键字:

    改变

    self.test1= Radiobutton(master, text="test1", variable=0, value=1, )
    

    self.test1= Radiobutton(master, text="test1", variable=self.iconnum, value=1, )
    

    这将在IntVar 中记录选定的值。 然后在icon_switcher函数中可以调用self.iconnum.get()获取选中的值。

    【讨论】:

    • 我刚刚添加了你所说的并且发生了这种情况,我的问题是我不知道如何调用 master.iconbitmap('favicon.ico') if iconnum == 1
    • @nooby 您是否将所有必需的Radiobutton 变量更改为self.iconnum?我无法在我的电脑上重现 2 值错误。关于icon_switcher,您可以使用ifstatement 来检查iconnum 值。例如:if self.iconnum.get() == 1: self.master.iconbitmap("icon1.png")
    • 不,我没有改变所有这些,我的 IDE (pycharm) 说这个 Unresolved attribute reference 'master' for class 'GUI'
    • 尝试 self.master.iconbitmap("icon1.png")
    • 更改 Radiobuttons 1 到 4 的变量。同时在定义 __init__() 后添加 self.master = master
    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多