gpt4 book ai didi

Python 3.x _tkinter.TclError : bad window path name ".!toplevel"

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:19 26 4
gpt4 key购买 nike

我想在 tkinter 中做一个弹出窗口小部件,用户可以在其中输入一些数据。但是在测试时我遇到了以下错误:

  File "returnWeeksGUI_v1.py", line 363, in __init__
self.content = program.popup("Add_label")
File "returnWeeksGUI_v1.py", line 296, in popup
self.w=popupWindow(self.master,popup_type)
File "returnWeeksGUI_v1.py", line 42, in __init__
self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
File "C:\Users\Fernanda\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2369, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Users\Fernanda\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: bad window path name ".!toplevel"

已经调试和编辑了3天的代码,但仍然没有找到合适的解决方案。

<小时/>

代码

我有一些导入和变量:

import tkinter as tk

models = {
"text":["TXT",50,50],
"DD":["DD",50,50],
"MM":["MM",50,50],
"AA":["AA",50,50],
"MC":["MC",50,50],
"DP":["DP",50,50],
}

我有一个 tkinter 类(class):

class App(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.master = master

#This class has some variables:
self.pu= program.popup("Add_label")

def popup(self,popup_type):
self.w=popupWindow(self.master,popup_type)
self.master.wait_window(self.w.top)

我还有一个从 a question in StackOverflow 获取的弹出代码,我根据自己的目的进行了修改。它显示了用户必须完成/选择的一些单选按钮/条目。

class popupWindow(object):
def __init__(self,master,win_type):
top=self.top=tk.Toplevel(master)
if str(win_type) == "Add_label":
"""
Here goes a large piece of code that is irrelevant.
"""
# Until I have this set of statements which break the program.
self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
self.bu.pack()

elif str(win_type) == "Add_box":
#...
self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
self.bu.pack()

elif str(win_type) == "Delete_label":
#...
self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
self.bu.pack()

elif str(win_type) == "Delete_box":
#...
self.bu=tk.Button(top,text='Ok',command=self.cleanup(win_type))
self.bu.pack()

def cleanup(self,win_type):
#...
# At the end, a destroy method is called.
self.top.destroy()

实例是这样的:

root = tk.Tk()
program = App(root)
program.mainloop()

弹出窗口不是由 __init__() 函数中的变量触发的。事实上,当按下某个按钮时它就会出现。不过,我在这里用一个变量代替了它,因为这样会更容易分析。触发弹出窗口的按钮很好。我之前查过。

<小时/>

如果有人能提供帮助,我将不胜感激。谢谢。

最佳答案

您现在正在创建按钮期间调用 self.cleanup(win_type) ,从而破坏了您尝试添加按钮的窗口。一种解决方案是将其编写为 command=lambda: self.cleanup(win_type),以便将调用推迟到实际单击按钮为止。 – Jasonharper 的评论回答

关于Python 3.x _tkinter.TclError : bad window path name ".!toplevel",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54428928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com