简体   繁体   English

错误:_tkinter.TclError:错误的窗口路径名“.!button”

[英]Errror: _tkinter.TclError: bad window path name ".!button"

So today I tried to use python classes for the first time, to remove the excessive use of global keyword.所以今天我第一次尝试使用python类,去掉全局关键字的过度使用。 I am trying to create a tkinter window in which, when we click one button it removes the clicked button and replaces it with a new button.我正在尝试创建一个 tkinter 窗口,在该窗口中,当我们单击一个按钮时,它会删除单击的按钮并将其替换为新按钮。 And when we click it again, it removes this button and replaces the old (first) button and this should cycle through out...当我们再次单击它时,它会删除此按钮并替换旧的(第一个)按钮,这应该循环...

This is my code which I made:这是我制作的代码:

# ================= Importing Modules ===================
from tkinter import *
import tkinter as tk

# ====================================================
class Test():
 
    # ============= Play Button Click =============
    def fun1(self):
            self.hi.destroy()
            self.he.place(x=350,y=340)

    # ============ Pause Button Click =============
    def fun2(self):
            self.he.destroy()
            self.hi.place(x=350,y=340)
            
    # ============ Player Window ================
    def __init__(self):
        self.root = Tk()
        self.root.geometry('700x400')
        self.root.resizable(0,0)
        self.root["bg"] = "black"
        
        self.hi = tk.Button(self.root, text="button 1", bg="white", bd=0, command=lambda: self.fun1() , relief=RIDGE)
        self.hi.place(x=350,y=340)

        self.he = tk.Button(self.root, text="button 2", bg="white", bd=0,  command=lambda: self.fun2() , relief=RIDGE)
        self.root.mainloop()

# ============== Calling ===========

if __name__ == '__main__':
        Test()

    

But Instead of the desired output, sadly, I got this error:但遗憾的是,我得到了这个错误,而不是所需的输出:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:/XXX/XXX/Desktop/test.py", line 29, in <lambda>
    self.he = tk.Button(self.root, text="button 2", bg="white", bd=0,  command=lambda: self.fun2() , relief=RIDGE)
  File "C:/XXX/XXX/Desktop/test.py", line 16, in fun2
    self.hi.place(x=350,y=340)
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2477, in place_configure
    self.tk.call(
_tkinter.TclError: bad window path name ".!button"

SO MY QUESTION IS:所以我的问题是:

doubt1 = Any idea what I am doing wrong?
doubt2 = Or isn't this possible?
if doubt1 or doubt2:
   Please explain it...
elif:
   Please tell me a better alternative or idea, how to do this efficiently.
else:
   Note: I have researched so many questions. Nothing helped me out. Especially ---|
                                                                                   ↓

ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ this one. ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ 这个

You're destroying self.hi and then later trying to call place on the destroyed button.您正在破坏self.hi ,然后尝试在被破坏的按钮上调用place Once a widget has been destroyed you can no longer use it.一旦小部件被销毁,您将无法再使用它。

If you want to keep cycling the buttons, don't destroy them.如果您想继续循环按钮,请不要破坏它们。 Since you are using place , you can use self.hi.place_forget() and self.he.place_forget() to remove the buttons from view without destroying them.由于您使用的是place ,因此您可以使用self.hi.place_forget()self.he.place_forget()从视图中删除按钮而不破坏它们。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM