[英]Python tkinter: error _tkinter.TclError: bad window path name ".!button2"
I am making a homescreen for my python tkinter program and I am having a strange error with my toggle button.我正在为我的 python tkinter 程序制作一个主屏幕,但我的切换按钮出现了一个奇怪的错误。
I defined 2 functions: The first one called self.do_on()
destroys the off button and places the on button.我定义了 2 个函数:第一个名为self.do_on()
的函数破坏了关闭按钮并放置了开启按钮。 The second one called self.do_off()
destroys the on button and places the off button.第二个调用self.do_off()
销毁 on 按钮并放置 off 按钮。
The off button is placed by default.默认情况下放置关闭按钮。
The program successfully execute the self.do_off()
function and turns the off button to the on one when the off button is pressed.程序成功执行self.do_off()
函数,并在按下关闭按钮时将关闭按钮变为打开按钮。 But then when I press the one button that has been placed, it just destroys the on button and returns an error.但是当我按下已放置的一个按钮时,它只会破坏 on 按钮并返回错误。
Here's the program:这是程序:
from tkinter import *
from tkinter import ttk
import tkinter as tk
import time
def isNumber():
try:
int(root.enteredTime)
return True
except ValueError:
return False
def callback1(var, index, mode):
root.enteredTime = str(root.entryTime.get()).replace(":", "")
if isNumber() == False:
print(root.changes)
print(root.changes[-1])
root.entryTime.delete(0,'end')
root.entryTime.insert('end', root.changes[-1])
else:
if len(root.enteredTime) <= 2:
root.entryTime.delete(0,'end')
root.entryTime.insert('end', root.enteredTime)
root.entryTime.insert('end', ':')
if len(root.enteredTime) == 4:
root.enteredTimeO = root.entryTime.get()
if len(root.enteredTime) >= 4:
root.focus()
root.entryTime.delete(0,'end')
root.entryTime.insert('end', root.enteredTimeO)
root.adds = root.adds +1
root.changes.append(str(root.entryTime.get()))
class root(Tk):
def __init__(self):
super(root, self).__init__()
self.off = PhotoImage(file = "OFF.png")
self.on = PhotoImage(file = "ON.png")
self.title("Chess Clock")
self.minsize(1539,800)
#self.wm_iconbitmap(r"")
self.windowBG = '#313131'
self.state('zoomed')
self.configure(bg=self.windowBG)
self.adds = 0
self.changes = []
self.createHomeScreen()
def do_off(self):
self.onBtn.destroy()
self.offBtn.place(x=900, y=390)
def do_on(self):
self.offBtn.destroy()
self.onBtn.place(x=900, y=390)
def createHomeScreen(self):
self.bg2 = Label(self, width=185, height=45, bg='#252525')
self.bg2.place(x=120, y=85)
Label(self, text='Time per player:', bg='#252525', fg='#AAA4A6', font ="Gadugi 30 bold").place(x=170, y=140)
Label(self, text='Extra Seconds:', bg='#252525', fg='#AAA4A6', font ="Gadugi 30 bold").place(x=170, y=240)
Label(self, text='Extra Seconds are added every move.', bg='#252525', fg='#656565', font ="Gadugi 15").place(x=185, y=300)
Label(self, text='Sound Notifications:', bg='#252525', fg='#AAA4A6', font ="Gadugi 30 bold").place(x=170, y=380)
Label(self, text='A sound notification will be emitted when clock is switched from a player to another one, when there is 30 or 10 seconds left ', bg='#252525', fg='#656565', font ="Gadugi 15").place(x=185, y=475)
Label(self, text='and also when the game is over.', bg='#252525', fg='#656565', font ="Gadugi 15").place(x=185, y=500)
self.setTime = StringVar()
self.entryTime = Entry(self, width=5, textvariable = self.setTime, font="Gadugi 30", background='#252525', fg='white', justify='center', insertbackground='white', borderwidth = 0, highlightthickness = 0)
self.entryTime.place(x=1000, y=140)
self.entryTime.focus()
self.setTime.trace_add("write", callback1)
self.onBtn = Button(self, image=self.on, bd=0, borderwidth = 0, highlightthickness = 0, command=self.do_off)
self.offBtn = Button(self, image=self.off, bd=0, borderwidth = 0, highlightthickness = 0, command=self.do_on)
self.offBtn.place(x=900, y=390)
root=root()
root.mainloop()
Here's the error:这是错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "d:\Users\Jean Paul\OneDrive\Programming\Programs\Prog 6 - Chess Clock\test.py", line 56, in do_off
self.offBtn.place(x=900, y=390)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 2448, in place_configure
self.tk.call(
_tkinter.TclError: bad window path name ".!button2"
Here are the off and on buttons images:以下是关闭和开启按钮的图片:
ON.png
: ON.png
:
OFF.png
: OFF.png
:
Here the code posted in the question with in the comments suggested change to replace .destroy()
with .place_forget()
.在这里,问题中发布的代码在评论中建议更改为将.destroy()
替换为.place_forget()
。 This solves the described problem:这解决了所描述的问题:
# https://stackoverflow.com/questions/72465414/python-tkinter-error-tkinter-tclerror-bad-window-path-name-button2
from tkinter import Tk, PhotoImage, Label, StringVar, Entry, Button
import time
def isNumber():
try:
int(root.enteredTime)
return True
except ValueError:
return False
def callback1(var, index, mode):
root.enteredTime = str(root.entryTime.get()).replace(":", "")
if isNumber() == False:
print(root.changes)
print(root.changes[-1])
root.entryTime.delete(0,'end')
root.entryTime.insert('end', root.changes[-1])
else:
if len(root.enteredTime) <= 2:
root.entryTime.delete(0,'end')
root.entryTime.insert('end', root.enteredTime)
root.entryTime.insert('end', ':')
if len(root.enteredTime) == 4:
root.enteredTimeO = root.entryTime.get()
if len(root.enteredTime) >= 4:
root.focus()
root.entryTime.delete(0,'end')
root.entryTime.insert('end', root.enteredTimeO)
root.adds = root.adds +1
root.changes.append(str(root.entryTime.get()))
class root(Tk):
def __init__(self):
super(root, self).__init__()
self.off = PhotoImage(file = "OFF.png")
self.on = PhotoImage(file = "ON.png")
self.title("Chess Clock")
self.minsize(1725,800)
# self.wm_iconbitmap(r"")
self.windowBG = '#313131'
# self.state('zoomed') # gives an error
self.state('normal')
self.configure(bg=self.windowBG)
self.adds = 0
self.changes = []
self.createHomeScreen()
def do_off(self):
self.onBtn.place_forget() # stop to show the placed button
# self.onBtn.destroy() will make the button unavailable
self.offBtn.place(x=900, y=390)
def do_on(self):
self.offBtn.place_forget() # stop to show the placed button
self.onBtn.place(x=900, y=390)
def createHomeScreen(self):
self.bg2 = Label(self, width=185, height=45, bg='#252525')
self.bg2.place(x=120, y=85)
Label(self, text='Time per player:',
bg='#252525', fg='#AAA4A6', font ="Gadugi 30 bold" ).place(x=170, y=140)
Label(self, text='Extra Seconds:',
bg='#252525', fg='#AAA4A6', font ="Gadugi 30 bold" ).place(x=170, y=240)
Label(self, text='Extra Seconds are added every move.',
bg='#252525', fg='#656565', font ="Gadugi 15" ).place(x=185, y=300)
Label(self, text='Sound Notifications:',
bg='#252525', fg='#AAA4A6', font ="Gadugi 30 bold" ).place(x=170, y=380)
Label(self, text='A sound notification will be emitted when clock is switched from a player to another one, when there is 30 or 10 seconds left ',
bg='#252525', fg='#656565', font ="Gadugi 11" ).place(x=185, y=475)
Label(self, text='and also when the game is over.',
bg='#252525', fg='#656565', font ="Gadugi 15" ).place(x=185, y=500)
self.setTime = StringVar()
self.entryTime = Entry(self, width=5, textvariable = self.setTime,
font="Gadugi 30", background='#252525', fg='white', justify='center',
insertbackground='white', borderwidth = 0, highlightthickness = 0)
self.entryTime.place(x=1000, y=140)
self.entryTime.focus()
self.setTime.trace_add("write", callback1)
self.onBtn = Button(self, image=self.on, bd=0, borderwidth = 0, highlightthickness = 0, command=self.do_off)
self.offBtn = Button(self, image=self.off, bd=0, borderwidth = 0, highlightthickness = 0, command=self.do_on)
self.offBtn.place(x=900, y=390)
root=root()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.