In this video I’ll show you how to dynamically resize your button text when you resize your app with Tkinter and Python.

In the last video I showed you how to resize buttons automatically whenever a user changes the size of your app manually. In this video I’ll show you how to do the same thing with your text size.

We’ll do this by binding the root window Configure to a function that grabs the width and height of the app and uses those measurements to resize the text size of our button.

Python Code: button_text.py
(Github Code)

from tkinter import *

root = Tk()
root.title('Codemy.com - Resize Button Text')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("500x500")


Grid.columnconfigure(root, index=0, weight=1)
Grid.rowconfigure(root, 0, weight=1)
button_1 = Button(root, text="Button 1!")
button_1.grid(row=0, column=0, sticky="nsew")

def resize(e):
	# Grab the app width and divide by 10
	size = e.width / 10
	# Change our button text size
	button_1.config(font=("Helvetica", int(size)))

	# Mess with height
	height_size = e.height / 10
	if e.height <= 300:
		button_1.config(font=("Helvetica", int(height_size)))		

	'''
	if e.height <= 300 and e.height > 200:
		button_1.config(font=("Helvetica", 30))
	elif e.height < 200 and e.height > 100:
		button_1.config(font=("Helvetica", 20))		 		
	elif e.height < 100:
		button_1.config(font=("Helvetica", 10))		 		
	'''

# Bind the app 
root.bind('', resize)

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