Reputation: 186
I am hoping to create a button to completely reset a Tkinter window as if the program has been run from scratch. Here is my current way of doing this. However it is not working as hoped.
from tkinter import *
master = Tk()
def do_something_():
#*performing a function on widget*
DoThing = Button(master, text='Do Something',command=do_something_).pack(pady=10)
clearall = Button(master, text='reset', command=resetAll).pack(pady=10)
def resetAll():
master.destroy()
master = Tk()
mainloop()
Is there any way to completely reset the window?
Upvotes: 2
Views: 18737
Reputation: 142641
You can create function which create Frame
and put widgets in this frame. And then you can put frame in window.
When you press button then you can destroy()
this frame to remove all widgets and you can run the same function to create widgets again. Or you can run different function to create different frame with widgets - so you can replace content in window.
from tkinter import *
# --- functions ---
def create_frame(master):
print("create frame")
frame = Frame(master)
b = Button(frame, text='Do Something')
b.pack(pady=10)
clearall = Button(frame, text='reset', command=reset_all)
clearall.pack(pady=10)
return frame
def reset_all():
global frame
frame.destroy()
frame = create_frame(master)
#frame = create_different_frame(master)
frame.pack()
# --- main ---
master = Tk()
frame = create_frame(master)
frame.pack()
mainloop()
BTW: if you do var = Widget().pack()
then you assign None
to var
and you have no access to Widget - ie. you can't detroy it. You have to do it in two steps
var = Widget()
var.pack()
if you don't need access to widget then you don't need variable
Widget().pack()
And when you have access to all widgets then you can change settings (ie. clear text) in every widget instead of destroying all widgets.
Upvotes: 5
Reputation:
You can create a canvas (or frame) and then make the DoThing and clearall buttons have the canvas (or frame) as their master widget. You can then make the resetAll subroutine destroy the canvas (or frame). This will then destroy all of the canvas's child-widgets as well.
Note: I also fixed some syntax errors in your code (e.g. you defined the resetALL subroutine after referencing it.)
The code:
from tkinter import *
master = Tk()
def do_something_():
print('do something') #I added this so that i can run the code with no errors
#*performing a function on widget*
def resetAll():
canvas.destroy() #destroys the canvas and therefore all of its child-widgets too
canvas = Canvas(master)
canvas.pack()
#creates the cnvas
DoThing = Button(canvas, text='Do Something',command=do_something_).pack(pady=10)
#its master widget is now the canvas
clearall = Button(canvas, text='reset', command=resetAll).pack(pady=10)
#its master widget is now the canvas
master.mainloop()
I ran this code.
This was the GUI before i clicked the 'reset' button:
This was the GUI after i clicked the 'reset' button:
As you can see it worked. The canvas's child-widgets (the buttons) were destroyed because the canvas was destroyed.
Upvotes: 0