I am making a homescreen for my python tkinter program and I am having a strange error with my toggle button.
I defined 2 functions: The first one called self.do_on()
destroys the off button and places the on button. The second one called self.do_off()
destroys the on button and places the off button.
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. But then when I press the one button that has been placed, it just destroys the on button and returns an error.
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
:
OFF.png
:
Here's a video showing the problem.
CodePudding user response:
Here the code posted in the question with in the comments suggested change to replace .destroy()
with .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()