The Wayback Machine - https://web.archive.org/web/20240711150838/https://www.geeksforgeeks.org/dynamically-resize-buttons-when-resizing-a-window-using-tkinter/
Open In App

Dynamically Resize Buttons When Resizing a Window using Tkinter

Last Updated : 04 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Python GUI – tkinter

Button size is static, which means the size of a button cannot be changed once it is defined by the user. The problem here is while resizing the window size, it can affect the button size problem. So the solution here is, make a dynamic button, which means the button size will change as per window size.

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

Step 1#: Create Normal Tkinter Window

Python3




# Import module
from tkinter import *
   
# Create object
root = Tk()
   
# Adjust size
root.geometry("400x400")
   
# Execute tkinter
root.mainloop()


Output:-

Step 2#: Add Buttons and set the grid.

Syntax:

Button(Object Name,text="Enter Text")

What is grid?

The grid() geometry manager organizes widgets in a table-like structure in the parent widget. The master widget is split into rows and columns, and each part of the table can hold a widget. It uses column, column span, ipadx, ipady, padx, pady, row, row span, and sticky.

Syntax:

Object_name.grid(row=row value,column=column value,**attributes)

Python3




# Import module
from tkinter import *
 
# Create object
root = Tk()
 
# Adjust size
root.geometry("500x500")
 
# Create Buttons
button_1 = Button(root,text="Button 1")
button_2 = Button(root,text="Button 2")
 
# Set grid
button_1.grid(row=0,column=0)
button_2.grid(row=1,column=0)
 
# Execute tkinter
root.mainloop()


Output:

Add Two Buttons

Step 3#: Set Columnconfigure & Rowconfigure for resize.

For the user interface to resize then, we’ll need to specify a positive weight to the columns that we’d like to expand. This is done using the Columnconfigure and Rowconfigure methods of the grid. This weight is relative. If two columns have the same weight, they’ll expand at the same rate.

What to do if the cell is larger than widget. By default, with sticky=”, widget is centered in its cell. sticky may be the string concatenation of zero or more of N, E, S, W, NE, NW, SE, and SW, compass directions indicating the sides and corners of the cell to which widget sticks. 

“NSEW” means N+S+E+W

Code:- 

Python3




# Import module
from tkinter import *
 
# Create object
root = Tk()
 
# Adjust size
root.geometry("500x500")
 
# Specify Grid
Grid.rowconfigure(root,0,weight=1)
Grid.columnconfigure(root,0,weight=1)
 
Grid.rowconfigure(root,1,weight=1)
 
# Create Buttons
button_1 = Button(root,text="Button 1")
button_2 = Button(root,text="Button 2")
 
# Set grid
button_1.grid(row=0,column=0,sticky="NSEW")
button_2.grid(row=1,column=0,sticky="NSEW")
 
# Execute tkinter
root.mainloop()


Output:

 



Previous Article
Next Article

Similar Reads

How To Dynamically Resize Button Text in Tkinter?
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.
3 min read
How to Dynamically Resize Background Images - Tkinter
You might surely be worried about how to resize your background image with the change in screen size. In this article, We are creating an advanced-level GUI application using the Tkinter module using Python in which you need to resize the app window various times and dynamically resize background images. Modules Required: Tkinter: As the GUI applic
6 min read
PyQt5 – How to stop resizing of window | setFixedSize() method
In this article, we will see how to stop resizing of the main window. While making a window, we get options like going full screen and using cursor to change its size. By using setFixedSize() method we can prevent the resizing of the image. Syntax : self.setFixedSize(width, height) Argument : It takes two integer as argument i.e width and height. A
1 min read
PYGLET – Setting Size / Resizing of Window
In this article we will see how we can set the size of the window in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill an en
2 min read
Allowing resizing window in PyGame
In this article, we will learn How to allow resizing a PyGame Window. Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI, and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the
2 min read
How to resize Image in Python - Tkinter?
Prerequisite: TkinterPIL Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using
2 min read
How to resize an Entry Box by height in Tkinter?
In this article, we shall look at how can one resize the height of an Entry Box in Python Tkinter. An Entry Widget is a widget using which a user can enter text, it is similar to HTML forms. In Tkinter, the Entry widget is a commonly used Text widget, using which we can perform set() and get() methods. But the developer might always find it annoyin
3 min read
PyQtGraph – Resize Plot Window
In this article we will see how we can resize the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and second is t
2 min read
Image resizing using Seam carving using OpenCV in Python
Seam carving is an effective image processing technique with the help of which an image can be resized without removing important elements from the image. The basic approach is to find all the continuous pixels with low energy from left to right or from top to bottom. After the region is selected, it is removed from the original image, leaving only
2 min read
How to make Rounded buttons in Tkinter
Tkinter is a Python module that is used to create GUI (Graphical User Interface) applications with the help of a variety of widgets and functions. Like any other GUI module, it also supports images i.e you can use images in the application to make it more attractive. In this article, we will discuss How to make a Rounded Button in Tkinter, There is
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg