In this video I’ll show you how to limit the number of new windows you create with your Tkinter app.
I’ve shown you how to open new windows in your app before, but in this video I’ll show you how to specify the exact number of new windows your app can open.
We’ll do it using a good old fashioned python counter and some if/else logic.
Python Code: windows.py
(Github Code)
from tkinter import * from tkinter import messagebox root = Tk() root.geometry("300x200") root.title('Learn To Code at Codemy.com') root.iconbitmap('c:/gui/codemy.ico') global counter counter = 1 def open(): global counter # Create counter logic if counter < 4: top = Toplevel() top.geometry("300x200") top.title('New Window') top.iconbitmap('c:/gui/codemy.ico') my_label = Label(top, text="New Window!", font=("Helvetica, 24")) my_label.pack(pady=50, padx=50) counter +=1 else: messagebox.showinfo("Error", "Hey! You've already opened a new window!") my_button = Button(root, text="Open Window", command=open) my_button.pack(pady=50, padx=50) mainloop()
Add comment