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()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...