Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Use Bitmap images in Button in Tkinter?
Next article icon

Prerequisite:Python GUI – tkinter,Dynamically Resize Buttons When Resizing a Window using Tkinter

In this article, we will see how to make the button text size dynamic. Dynamic means whenever button size will change, the button text size will also change. In Tkinter there is no in-built function, that will change the button text size dynamically.

Approach:

  • Create button and set sticky to all direction
  • Set bind, what bind will do, whenever button size change it will call resize function that we will create later.
  • Inside the resize function, we will have a different condition, depends on the main window geometry/size.
  • Set row and column configure

Let's understand with step-by-step implementation:

Step 1:Creates a normal Tkinter window.

Python3
# Import modulefromtkinterimport*# Create objectroot=Tk()# Adjust sizeroot.geometry("400x400")# Execute tkinterroot.mainloop()

Output:

Step 2:Create a button inside the main window.

Python3
# Import modulefromtkinterimport*# Create objectroot=Tk()# Adjust sizeroot.geometry("400x400")# Create Buttonsbutton_1=Button(root,text="Button 1")# Set gridbutton_1.grid(row=0,column=0)# Execute tkinterroot.mainloop()

Output:

Step 3:Resizing the button text size

Inside the resize function, the "e" value will tell the main window width and height.

Python3
# resize button text sizedefresize(e):# get window widthsize=e.width/10# define text size on different condition# if window height is greater# than 300 and less than 400 (set font size 40)ife.height<=400ande.height>300:button_1.config(font=("Helvetica",40))# if window height is greater than# 200 and less than 300 (set font size 30)elife.height<300ande.height>200:button_1.config(font=("Helvetica",30))# if window height is less than 200 (set font size 40)elife.height<200:button_1.config(font=("Helvetica",40))

Below is the full implementation:

Python3
# Import modulefromtkinterimport*# Create objectroot=Tk()# Adjust sizeroot.geometry("400x400")# Specify GridGrid.columnconfigure(root,index=0,weight=1)Grid.rowconfigure(root,0,weight=1)# Create Buttonsbutton_1=Button(root,text="Button 1")# Set gridbutton_1.grid(row=0,column=0,sticky="NSEW")# resize button text sizedefresize(e):# get window widthsize=e.width/10# define text size on different condition# if window height is greater# than 300 and less than 400 (set font size 40)ife.height<=400ande.height>300:button_1.config(font=("Helvetica",40))# if window height is greater than# 200 and less than 300 (set font size 30)elife.height<300ande.height>200:button_1.config(font=("Helvetica",30))# if window height is less# than 200 (set font size 40)elife.height<200:button_1.config(font=("Helvetica",40))# it will call resize function# when window size will changeroot.bind('<Configure>',resize)# Execute tkinterroot.mainloop()

Output:


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp