简体   繁体   中英

How to reset window grid contents on Python Tkinter when button is pushed?

I am working on a basic application, and want people to be able to follow prompts on the same window, like when installing an application and clicking next... and then finish etc...

I have a basic window with a button, but how do I clear the contents of the grid so I can repopulate it?

import tkinter as tk

def nextbtn():
    #clear window to start fresh
    #then change the text
    mybrain="notsure"


root = tk.Tk()
root.configure(background='#e3e5e8')

root.title("Test Utility")
root.geometry("500x400")
tk.Label(root, text=
         """This is my test application. Press Next to continue.

            """.format(localIp), background='#e3e5e8').grid(row=0, column=2,columnspan = 2)
tk.Button(text='Next',command=nextbtn, background='#e3e5e8').grid(row=3, column=3, sticky=tk.W, pady=4)

root.eval('tk::PlaceWindow . center')
w = 500 # width for the Tk root
h = 400 # height for the Tk root

# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen

# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

# set the dimensions of the screen 
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

root.mainloop() # starts the mainloop

what I would like to do is when button is pushed clear grid, and then replace the text "this is my test app..." with "hello world.".

You do not need to clear the whole grid to update a label.

You should save a reference to your label and update it's text attribute when the button is pressed.

def nextbtn():
    my_label['text'] = "Hello World"

my_label = tk.Label(root, text="This is my test application. Press Next to continue.")

If you will be making significant changes in each phase of your application, then you should use a frame to hold your widgets and .destroy() it after each phase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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