In this video we’ll start to learn Object Oriented Programming for Tkinter and Python.

There aren’t a ton of OOP tutorials for Tkinter. In this playlist we’ll change that!

We’ll learn how to used Classed Based Programming for Tkinter, and in this video we’ll create our basic starter code and use the Label and Button widgets to build a simple app.

Python Code: classes.py
(Github Code)


from tkinter import *


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

		# Title, icon, size
		self.title("Tkinter.com - Object Oriented Programming!")
		self.iconbitmap('images/codemy.ico')
		self.geometry('700x450')

		# Create Status Variable
		self.status = True

		# Create some widgets
		self.my_label = Label(self, text="Hello World!", font=("Helvetica", 42))
		self.my_label.pack(pady=20)

		self.my_button = Button(self, text="Change Text", command=self.change)
		self.my_button.pack(pady=20)

		# Create a frame outside this function
		My_frame(self)

	def change(self):
		if self.status == True:
			self.my_label.config(text="Goodbye World!")
			self.status = False
		else:
			self.my_label.config(text="Hello World!")
			self.status = True

class My_frame(Frame):
	def __init__(self, parent):
		super().__init__(parent)

		# Put this sucker on the screen
		self.pack(pady=20)
		# Create a few buttons
		self.my_button1 = Button(self, text="Change", command=parent.change)
		self.my_button2 = Button(self, text="Change", command=parent.change)
		self.my_button3 = Button(self, text="Change", command=parent.change)

		self.my_button1.grid(row=0, column=0, padx=10)
		self.my_button2.grid(row=0, column=1, padx=10)
		self.my_button3.grid(row=0, column=2, padx=10)




# 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...