Reputation: 7
I'm currently trying to create a command centre to integrate with one of my other python programs, however, I am quite new to Tkinter, and as such, I was wondering if there was a way to "reset" a window that has widgets in it so that I can have a clean window to place new widgets in, without storing any data or using system resources. Here is the code I currently have:
import tkinter
tk = tkinter
window = tk.Tk()
def login():
window.title("SCC: Login")
header = tk.Label(window, text = "Please Login").pack()
frame = tkinter.Label(window, text = "Username").pack()
frame = tkinter.Entry(window).pack()
frame = tkinter.Label(window, text = "Password").pack()
frame = tkinter.Entry(window).pack()
frame = tkinter.Checkbutton(window, text = "Keep Me Logged In").pack()
frame = tkinter.Button(window, text = "Login", command = mainMenu).pack()
window.mainloop()
def mainMenu():
()
window.title("SCC")
tk.Label(window, text = "Welcome to the Sentinel Command Center").pack()
tk.Button(window, text = "Network Utilities", fg = "orange").pack()
tk.Button(window, text = "Image Recognition", fg = "orange").pack()
tk.Button(window, text = "Voice Recognition", fg = "orange").pack()
window.mainloop()
login()
also as a side note i am fully aware the current password form is non functional, its just a placeholder for now until i find a better method of authorization for the code.
Upvotes: 0
Views: 1357
Reputation: 1920
This function can be used to remove all existing widgets:
def wempty(w):
for w in w.winfo_children():
w.destroy()
Upvotes: 0
Reputation: 7
Update: I found a functional if a convoluted method to doing this, just by creating separate windows, then withdrawing the login window once the main menu has been called. Here is the code if you want to use it:
mainWindow = tk.Tk()
loginWindow = tk.Tk()
version = ("v0.1")
def centerwindowLogin(width=300, height=200):
screen_width = loginWindow.winfo_screenwidth()
screen_height = loginWindow.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
loginWindow.geometry('%dx%d+%d+%d' % (width, height, x, y))
def centerwindowMain(width=300, height=200):
screen_width = mainWindow.winfo_screenwidth()
screen_height = mainWindow.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
mainWindow.geometry('%dx%d+%d+%d' % (width, height, x, y))
def login():
loginWindow.title("SCC v0.1")
header = tk.Label(loginWindow, text = "Please Login").pack()
tk.Label(loginWindow, text = "Username").pack()
tk.Entry(loginWindow).pack()
tk.Label(loginWindow, text = "Password").pack()
tk.Entry(loginWindow).pack()
tk.Checkbutton(loginWindow, text = "Keep Me Logged In").pack()
tk.Button(loginWindow, text = "Login", command = mainMenu).pack()
centerwindowLogin(300, 175)
loginWindow.mainloop()
def mainMenu():
loginWindow.withdraw()
mainWindow.title("SCC v0.1")
tk.Label(mainWindow, text = "Welcome to the Sentinel Command Center").pack()
tk.Button(mainWindow, text = "Network Utilities", fg = "orange").pack()
tk.Button(mainWindow, text = "Image Recognition", fg = "orange").pack()
tk.Button(mainWindow, text = "Voice Recognition", fg = "orange").pack()
centerwindowMain(500, 400)
mainWindow.mainloop()
login()
Upvotes: 0
Reputation: 385940
Arguably, the easiest way to reset it is to put all of your widgets inside a frame, and then create a function that creates the frame. Then, to do a reset you can destroy the current frame and call your function to recreate it.
Upvotes: 3