Set window size of in Tkinter through variables.

This tutorial will teach us how easily to set the Tkinter window size with dynamic variables in Python.

At first, I just had to import the Tkinter in my program pass. I am going to use the Tkinter name. I have written sree instead of Tkinter. It will save me time.

import tkinter as sree

Then I’ll have to create a variable. Any name can be given. I am giving it netWindow. This is the variable where I will be storing my window. we can write this netWindow= sree.Tk(). So I have created my first Window.

import tkinter as sree
netWindow= sree.Tk()

So I can write it down through a variable x=500 and y=300 . So till now, we have done this. This is our window and we have used the geometry method to change our. So, to do that, we have to use curly bracket just like this {}. We can use this, just write a f in front of this and thenĀ  insert variable. We can write netWindow.geometry(f”{x}x{y}”)

import tkinter as sree
netWindow= sree.Tk()
x=500
y=300
netWindow.geometry(f"{x}x{y}")

But if I run this program, nothing will happen. Because in Tkinter l have to run it in a main loop. I have to make my window in such a way that it will wait for events and activities. So to do that, I have to write this netWindow.mainloop().

import tkinter as sree
netWindow= sree.Tk()
x=500
y=300
netWindow.geometry(f"{x}x{y}")
netWindow.mainloop()

So, if I run it, you can see the output in my window.

Output:

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top