【问题标题】:Python 3.x _tkinter.TclError: bad window path name ".!toplevel"Python 3.x _tkinter.TclError:错误的窗口路径名“.!toplevel”
【发布时间】:2019-01-29 20:15:12
【问题描述】:

我希望在 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__() 函数中的变量触发的。事实上,它会在按下某个按钮时出现。但是,我在这里将其替换为变量,因为这样更易于分析。触发弹出窗口的按钮很好。我之前查过。


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

【问题讨论】:

  • 您正在调用self.cleanup(win_type) 现在,在创建按钮期间,从而破坏了您尝试添加按钮的窗口。一种解决方案是将其写为command=lambda: self.cleanup(win_type),以便将调用延迟到实际单击按钮为止。
  • 谢谢@jasonharper!错误不再出现!现在它又掉了一个:AttributeError: 'popupWindow' object has no attribute 'value'。但我应该能够应付!
  • @Brianprokpo456:您的class popupWindow 确实没有属性'value'。那么如何你能处理它?
  • 这是未显示的长代码的一部分。它具有“价值”属性。我只是没有把它放在问题中,因为我正在寻找修复另一个错误
  • @jasonharper 你已经在cmets上回答了我,考虑添加一个内容相同/相似的官方回答,这样我就可以接受了!

标签: python python-3.x tkinter toplevel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 2021-01-12
    • 2020-06-10
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多