【问题标题】:Custom Tkinter root.destroy exception自定义 Tkinter root.destroy 异常
【发布时间】:2022-12-20 23:29:47
【问题描述】:

我正在使用 customtkinter 库创建一个按钮。该按钮用于关闭程序。

下面是按钮的定义:

  exit_button = ctk.CTkButton(master=main_menu_frame,
    text="Exit",
    command=root.destroy,
    corner_radius=0,
    width=WIDTH-40-260,
    height=60,
    text_font=("century gothic", 16),
  )

如您所见,该命令等于 root.destroy。当我点击这个按钮时它真的关闭了窗口,但它也给出了一个例外。 这是例外:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 501, in clicked
    self.on_leave()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 485, in on_leave
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2903, in itemconfigure
    return self._configure(('itemconfigure', tagOrId), cnf, kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1636, in _configure    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!ctkframe2.!ctkbutton3.!canvas"

这是一些要测试的代码:

import tkinter as tk
import customtkinter as ctk

root = tk.Tk()

btn = ctk.CTkButton(master=root, text="EXIT", command=root.destroy).pack()

root.mainloop()

使用此代码,我得到了相同的异常。

【问题讨论】:

  • 请提供minimal reproducible example。该错误是由您对 on_leave 的定义引起的,但您没有提供该定义。
  • 你什么意思?我没有在我的代码中的任何地方使用过on_leave
  • @BryanOakley 该方法似乎是在位于site-packages 的某个模块(customtkinter)中定义的,这意味着它可能是通过pip 安装的
  • 我通过 pip 安装了自定义修补程序
  • 这是 customtkinter 库中的一个错误,按钮在它被销毁后被修改,现在已修复,上面的代码应该适用于 1.8 及更高版本!

标签: python tkinter customtkinter


【解决方案1】:

像这样尝试

import tkinter as tk
import customtkinter as ctk

root = tk.Tk()

def close_app():
   root.destroy()

btn = ctk.CTkButton(master=root, text="EXIT", command=close_app).pack()

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2016-07-27
    • 2015-02-05
    • 2011-02-11
    相关资源
    最近更新 更多