Updated June 23, 2023
Introduction to Tkinter Window Size
Window is the container of Tkinter, where we can place all our other widgets. If we want to give the size of our window, we can use the geometry method. The window is nothing but the object of Tkinter; we can assign different sizes to our window. This size will be in pixels and take horizontal and vertical size parameters as string values. We can also use the minsize method to give a size to our window in Tkinter.
We can use the geometry method or minsize method to set the size of the Tkinter window. Inside this, we must assign height, width, or value as the string. For a better understating, see the syntax below;
In the above syntax, we are calling this geometry method on the parent object, which is a Tkinter object here. We can assign the value of size as a string inside this method by using ‘x’ operator between the size.
This is another method we can use to size our window. Inside this method, we are giving two parameters here named width and height as follows. Let’s see one practice syntax for better understanding. See below;
In this way, we can use this in our program while creating a window in Tkinter.
How does Window Size Work in Tkinter?
As of now, we know we can provide size to our window by using geometry and midsize methods in Tkinter. But to use both these methods, we need to import the Tkinter module into our program because we can call this by using Tkinter object only.
Both these methods take height and width as a parameter directly or indirectly. Now we will see one example to understand it better and how they actually work internally. See below;
from tkinter import * parent = Tk() parent.minsize(width=100, height=100) parent.geometry("100x100") parent.mainloop()
In the above example, we are sizing our window using both methods. We use the import keyword followed by the library name to import this module.
- see: from Tkinter import *
After this, we will create a Tkinter object which will be called a parent object here. On this parent object, we can call both this method. We will discuss this in detail; see below;
- geometry (): We can call this method by using the Tkinter object ‘parent.’ It only takes one parameter, and this will be a string value separated by ‘x’ operator. Indirectly it takes height and width in a different format, we can say. Here we are assigning 100×100 as the size of the window.
- minsize (): This method is also used to size our window. It takes two parameters width and height. The first parameter is width, and the second we assign height here.
At last, we are calling mainloop () method; this method is used to initialize the window in Tkinter. This method is also called on the Tkinter object. This is very important to call. Otherwise, we will not be able to see the window.
Here is no constructor because these are itself a method that is called on the Tkinter object. while creating the object, we used no parameter constructor and call this method. Both these methods use the below constructor; after this, we can use or call them.
parent = Tk();
Methods are generally used to get the current value of the variable. If you want to see or fetch the current size of the window, then we can call the methods available in Tkinter. This method will help us to get the width and height of the window.
This method is used to get the current width of the window. This method can call by using the Tkinter object, so first create its object. Let’s see one simple example for a beginner to use this while programming;
import tkinter as tk parent = tk.Tk() width1 = parent.winfo_screenwidth()
2) winfo_screenheight() :
This method is used to get the current height of the window. This method can call by using the Tkinter object, so first create its object. Let’s see one simple example for a beginner to use this while programming;
import tkinter as tk parent = tk.Tk() height1 = parent.winfo_screenheight()
Some points to remember while using these methods;
- The geometry method takes only one parameter as input for the window size.
- The minsize method takes two parameters that specify the width and height of the window.
Lets us discuss the examples of Tkinter Window Size.
Example #1: Using Geometry Method
In this example, we use the geometry method to assign a size to our window in Tkinter. After that, we attach a button widget to it.
from tkinter import * def showval(): print (sli1.get()) parent = Tk() #geometry method called here parent.geometry("500x500") #button creation button = Button(parent, text = 'Click me !!', bg='yellow', fg = 'blue', height = 5, width = 10) button.pack() #initiizing window mainloop()
Example #2: Using Minsize Method
In this example, we are using the minsize method to assig size to our window in the tkinter. After that, we attach a button widget to it. Also, we are assigning some properties for buttons as well here, like color, height, etc.
from tkinter import * def showval(): print (sli1.get()) parent = Tk() #geometry parent.minsize(width=500, height=500) #button creation button = Button(parent, text = 'Click me !!', bg='yellow',fg = 'blue', height = 5, width = 10) button.pack() mainloop()
Using these methods, we can easily size or resize our GUI window, which is the main container in Tkiker, where we place all other stuff like buttons, labels, checkboxes, and other events. All other widgets on the screen will get adjusted according to the size of the window, and this will be platform-independent as well.
This is a guide to Tkinter Window Size. Here we discuss the definition of Tkinter Window Size; how does Window Size work in Tkinter? with sample code for better understanding. You may also have a look at the following articles to learn more –