Reputation: 559
I am trying to switch between two buttons with tkinter.
I have two buttons which trigger the functions. With changeOne in only want to show ModusAButton and delete ModusBButton. And the opposite for changeTwo
.
I got this error message: _tkinter.TclError: bad window path name ".!button2"
Whats the correct way to do this?
ModusAButton = Button(root, text="ModusA")
ModusBButton = Button(root, text="ModusB")
def changeOne():
ModusAButton.grid(row=1,column=0,sticky=W, padx=10,pady=10)
ModusBButton.destroy()
def changeTwo():
ModusBButton.grid(row=1,column=1,sticky=W, padx=10,pady=10)
ModusAButton.destroy()
ChangeOneButton = Button(root, text="ChangeOne",command=changeOne)
ChangeOneButton.grid(row=0,column=0,sticky=W, padx=10,pady=10)
ChangeTwoButton = Button(root, text="ChangeTwo",command=changeTwo)
ChangeTwoButton.grid(row=0,column=1,sticky=W, padx=10,pady=10)
Upvotes: 0
Views: 549
Reputation: 2711
.destroy()
gets rid of the button altogether. To only remove it temporarily, use grid_forget()
:
def changeOne():
ModusAButton.grid(row=1,column=0,sticky=W, padx=10,pady=10)
ModusBButton.grid_forget()
def changeTwo():
ModusBButton.grid(row=1,column=1,sticky=W, padx=10,pady=10)
ModusAButton.grid_forget()
Upvotes: 2