Introduction to Tkinter Window Size
Window are the containers of Tkinter, on which we can place all our other widgtes. If we want to give size of our window we can use geometry method. Window is nothing but the object of Tkinter we can assign different size to our window this size will be in pixels and will take horizontal and vertical size parameter as string value. We can also use minsize method to give size to our window in Tkinter.
We can use geometry method or minsize method to set the size of the Tkinter window. Inside this we need to assign height, width or value as the string. For better understating see syntax below;
In the above syntax we are calling this geometry method on parent object which is an Tkinter object here. We can assign value of size as string inside this method by using ‘x’ operator between the size.
This is the another method we can use to size our window. Inside this method we are giving two parameters here named as 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 window in Tkinter.
How does Window Size work in Tkinter?
As now we know that we can provide size to our window by using geometry method and midsize method in Tkinter. But in order to use both this methods, we need to import the Tkinter module into our program because we can call this by using Tkinter object only.
Both these methods takes 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 by using both the mentioned methods. First, we need to import the Tkinter module into our progam as we know this is used to create the GUI in python. To import this module we use import keyword followed by the library name.
- see : from Tkinter import *
After this we will create Tkinter object this 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 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 parameter width and height. Here we are calling this method on the Tkinter object because it can be access by its object only. The first parameter is width and the second we are assigning is 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 which is called on the Tkinter object. while creating the object we used no parameter constructor and call this methods. Both these methods use 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 methods 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 methods can call by using the Tkinter object so first create its object. Let’s see one simple example for 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 methods can call by using the Tkinter object so first create its object. Let’s see one simple example for 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;
- Geometry method takes only one parameter as input for size of window.
- Minsize method takes two parameter which specifies 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 are using geometry method to assig size to our window in Tkinter after that we are attaching button widget on 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 minsize method to assig size to our window in tkinter after that we are attaching button widget on it. Also we are assigning some property for button 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()
By the use of these methods, we can easily size or resize our GUI window which is the main container in Tkiker. Where we placed all other stuff like button, label, checkbox, and other events. All other widget on the screen will get adjust according to the size of 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 –