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