In this video we’ll learn how to create Popup Boxes using Object Oriented Code for Tkinter and Python.

Popup boxes, or messageboxes, are an important component of many GUI Apps.

Creating them in Tkinter using Object Oriented Code is super easy!

Python Code: class_popups.py
(Github Code)

from tkinter import *
from tkinter import messagebox

class App(Tk):
	def __init__(self):
		super().__init__()

		# Title, icon, size
		self.title("Tkinter.com - OOP Popup Boxes")
		self.iconbitmap('images/codemy.ico')
		self.geometry('700x450')

		# Create Widgets
		self.my_label = Label(self, text="Enter Your Name:", 
			font=("Helvetica", 24))
		self.my_label.pack(pady=20)

		self.my_entry = Entry(self, width=30, font=("Helvetica", 24))
		self.my_entry.pack(pady=20)

		self.my_button = Button(self, text="Popup", command=self.popup)
		self.my_button.pack(pady=20)
	
	# Create Popup Function
	def popup(self):
		#showinfo()
		#showwarning()
		#showerror ()
		#askquestion()
		#askokcancel()
		#askyesno ()
		#askretrycancel ()
		if self.my_entry.get():
			messagebox.showinfo("Hello", f"Hello {self.my_entry.get()}")
		else:
			messagebox.showerror("Error", "You Forgot To Type In Your Name!! Try Again")

# Define and instantiate our app
app = App()
app.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...