我一直希望在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 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__()
函数中的变量触发的。实际上,当按下某个按钮时,它会显示出来。但是,在这里我将其替换为变量,因为它更易于分析。触发弹出窗口的按钮很好。我已经检查过了。
如果有人可以提供帮助,我将不胜感激。谢谢。
答案 0 :(得分:0)
您现在在创建按钮期间正在调用self.cleanup(win_type),从而破坏了您要向其添加按钮的窗口。一种解决方案是将其写为command = lambda:self.cleanup(win_type),以便将调用推迟到实际单击按钮之前。 –杰森哈珀的评论答案