0

好的,所以我有这个很长的程序。我的主题有问题。我所说的主题是指我构建的一个特定功能,用于在 Tkinter 中激活时更改背景和文本颜色。这是代码的缩短版本:

 global theme
 theme = 0
 windows = []
 buttons = []
 labels = []
messageboxes = []

代码...

    def flavor_0():
    global theme
    for w in windows:
        w.config(bg = 'black')
    for l in labels:
        l.config(bg = 'black', fg = 'white')
    for b in buttons:
        b.config(activebackground = "grey", activeforeground = "white", bg = 'black', fg = 'white')
    for i in listboxes:
        i.config(bg = 'black', fg = 'white', relief = "ridge") 
    theme = 0

更多代码...(我在本节中定义了其他风格)

def set_theme():
    global theme
    global register
    if theme == 0:
        flavor_0()
    elif theme == 1:
        flavor_1()
    elif theme == 2:
        flavor_2()
    elif theme == 3:
        flavor_3()
    elif theme == 4:
        flavor_4()

更多代码

    thememenu = Tkinter.Menu(menubar,tearoff = 0)
    thememenu.add_command(label="Plain",command = flavor_0)
    thememenu.add_command(label="Mint", command = flavor_1)
    thememenu.add_command(label="Strawberry", command = flavor_2)
    thememenu.add_command(label="Banana", command = flavor_3)
    thememenu.add_command(label="Peanut", command = flavor_4)
    menubar.add_cascade(label="Flavor", menu = thememenu)

    def windowinator():
        new = Tkinter.Tk()
        windows.append(new)
        set_theme()

    windowinator = Tkinter.Button(root,text="New Window", command = windowinator)
    windowinator.pack()
    buttons.append(windowinator)

OK 终于搞定了。现在,当我运行它时,主题转换器可以工作。直到我使用“windowinator”。只要我之前打开的一个窗口没有关闭,就可以更改主题,没有任何问题。但是当我只关闭其中一个并尝试更改“主题”时:我得到一个普通的(无主题窗口)和这个尴尬的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\Ahmet\Desktop\Fobby\FOBBY.py", line 70, in flavor_3
    w.config(bg = 'black')
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1202, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1193, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: invalid command name "."

那么谁能告诉我到底出了什么问题?

4

1 回答 1

1

您应该只创建Tk该类的单个实例。看起来正在发生的事情是初始默认窗口(“。”)正在被破坏。Tkinter 并非设计为以这种方式使用。

如果您想要多个窗口,请创建Toplevel.

于 2013-03-08T18:46:40.647 回答