简体   繁体   English

为什么在 Tkinter 中尝试将标签小部件添加到屏幕时出现“错误的窗口路径名”错误?

[英]Why am I getting a "bad window path name" error when trying to add a label widget to the screen in Tkinter?

I'm creating a homework planner program using Tkinter, and I've run into a problem when trying to add an error label to the screen to show the user that they might have entered a duplicate assignment.我正在使用 Tkinter 创建一个家庭作业计划程序,在尝试向屏幕添加错误标签以向用户显示他们可能输入了重复的作业时遇到了问题。 Here's the constructor for the label (entFrame is where I put the user entries):这是标签的构造函数(entFrame 是我放置用户条目的位置):

duplicateLabel = Label(entFrame, text="Warning: the entered assignment is a duplicate!", bg=bgColor, font=regularFont)

When a user submits an assignment, a function runs that creates a new assignment object.当用户提交作业时,会运行一个创建新作业对象的函数。 In that function, I call a method of the assignment to check if it is a duplicate.在该函数中,我调用分配的方法来检查它是否重复。 In that method, I create a list of all the lines of a csv file where I store the assignments.在该方法中,我创建了一个 csv 文件的所有行的列表,用于存储分配。 Then, I check the new assignment's attributes to some of the stored attributes of previous assignments.然后,我检查新分配的属性到以前分配的一些存储属性。 If the conditional is true, I add the warning label to the screen.如果条件为真,我将警告标签添加到屏幕上。 Otherwise, I try to destroy the label to remove it from the screen to get rid of a previous warning from an old assignment.否则,我会尝试销毁标签以将其从屏幕上删除,以消除旧作业中的先前警告。

    def checkDuplicates(self):
        contents = []
        file = open("homeworkAssets/assignmentsFile", "r")
        for content in csv.reader(file):
            contents.append(content)
        file.close()
        for assignment in contents:
            if self.course == assignment[1] and self.dueDate == assignment[2] and self.details == assignment[3]:
                duplicateLabel.grid(row=0, column=0, columnspan=2)
            else:
                duplicateLabel.destroy()

My first thought was that the .destroy() method was deleting the Label object, although when I added a print statement after that last line, the console read: .!labelframe.!labelframe.!label6 which I assume means it hasn't been deleted.我的第一个想法是 .destroy() 方法正在删除 Label 对象,尽管当我在最后一行之后添加打印语句时,控制台显示: .!labelframe.!labelframe.!label6我认为这意味着它没有被删除。 When I run the program with an empty csv file and I add one assignment, and then a duplicate assignment, I receive this error message (sorry for the bad formatting, I couldn't figure out how to do it properly):当我使用空的 csv 文件运行程序并添加一个作业,然后添加一个重复的作业时,我收到此错误消息(抱歉格式错误,我无法弄清楚如何正确执行此操作):

File "/Users/max/Desktop/Coding/Python/Misc./homeworkPlannerRevised.py", line 212, in checkDuplicates duplicateLabel.grid(row=0, column=0, columnspan=2) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2493, in grid_configure self.tk.call( _tkinter.TclError: bad window path name ".!labelframe.!labelframe.!label6"

Why is this error occurring and how do I fix it?为什么会发生此错误,我该如何解决? Thanks in advance!提前致谢!

.destroy() removes widget from window (and memory) but it doesn't remove path from variable. .destroy()从窗口(和内存)中删除小部件,但不会从变量中删除路径。
print(duplicateLabel) shows path to object even if it is destroyed. print(duplicateLabel)显示对象的路径,即使它被破坏。

You run destroy() in loop - so one loop can destroy label and next loop may try to destroy it again - and this can make problem.您在循环中运行destroy() - 因此一个循环可以破坏标签,下一个循环可能会尝试再次破坏它 - 这可能会产生问题。

You could assign None to variable after destroying and check None before destroying.您可以在销毁后将None分配给变量,并在销毁前检查None

if duplicateLabel is not None:
    duplicateLabel.destroy()
    duplicateLabel = None

but if you destroy object then later you have to create it again但是如果您销毁对象,那么稍后您必须再次创建它

if duplicateLabel is None:
   duplicateLabel = Label(...)

But frankly you don't need destroy() but grid_forget() to hide widget without destroying.但坦率地说,你不需要destroy()而是grid_forget()来隐藏小部件而不破坏。

if self.course == assignment[1] and self.dueDate == assignment[2] and self.details == assignment[3]:
    duplicateLabel.grid(row=0, column=0, columnspan=2)
else:
    duplicateLabel.grid_forget()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到 tkinter ' bad window path name.?button2 ' 错误,我不知道为什么? - I am getting an error with tkinter ' bad window path name .!button2 ' and I am not sure why? 我在销毁按钮时收到错误 _tkinter.TclError: bad window path name “.!button” - I get the error _tkinter.TclError: bad window path name “.!button” when I destroy the button 为什么我在标签小部件中的文本周围出现难看的大括号? - Tkinter - Why am I getting ugly curly brackets around my text in the label widget? - Tkinter 为什么在尝试获取文本小部件的内容时收到 tkinter.TclError? - Why am I receiving a tkinter.TclError when trying to get the content of a text widget? _tkinter.TclError: 错误的窗口路径名 - _tkinter.TclError: bad window path name 为什么我在尝试从条目 object: Tkinter AttributeError: CustomClass instance has no attribute 'entry' 获取文本时收到此错误? - Why am I getting this error when trying to get text from an entry object: Tkinter AttributeError: CustomClass instance has no attribute 'entry'? 为什么我在尝试向 textRPG 添加陷阱时遇到此错误 - Why am i getting this error trying to add trap to my textRPG 尝试访问网站的域名时为什么会出现404错误,但是如果我的域名/住所能正常工作? - Why am I getting a 404 error when trying to access my website's domain name, yet if I domain name/home it works? Python tkinter:错误 _tkinter.TclError:错误的窗口路径名“.!button2” - Python tkinter: error _tkinter.TclError: bad window path name ".!button2" 为什么在定义变量时出现名称错误? - Why am I getting a name error when I defined the variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM