【问题标题】:Tkinter window configuring python error "of no return"Tkinter窗口配置python错误“不归路”
【发布时间】:2013-03-08 17:27:14
【问题描述】:

好的,所以我有这个很长的程序。我的主题有问题。我所说的主题是指我构建的一个特定功能,用于在 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)

好的,终于完成了。 现在,当我运行它时,主题转换器可以工作。直到我使用“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 "."

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

【问题讨论】:

  • 您的某些缩进不正确。

标签: python window themes tkinter


【解决方案1】:

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

如果您想要多个窗口,请创建 Toplevel 的实例。

【讨论】:

  • 现在我得到另一个错误:Tkinter 回调中的异常... TclError:无效的命令名称“.44532640”
  • @madprogramer:这意味着您正在尝试在某个已被破坏的小部件上运行命令。
  • 好的,那么是否有命令检查小部件何时被销毁并执行功能?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 2018-05-03
  • 1970-01-01
相关资源
最近更新 更多