When you start programming in python using Tkinter. You will find that the default form or dialog location is not appear as desired location. You will have to move it by mouse. And also in some cases you just need to show Tkinter window in center position on the screen. Find latest version of Python.
So, here SKOTechLearn will explain and describe step by step process to Set Tkinter Window Position and Size or Center Screen Tkinter in Python.
Easy Way to Use Tkinter Label in Python.
Let’s Start with following way:
By default tkinter is set in 200x200 height and width size.
In this syntax, you have to change width and height with desired number or integer.
Now lets see the example how its work?
This way you can set the size of your application form.
Python If Else Statement with Details Description and Example.
Here, you have to replace “Left” and “Top” with integer.
So, the simple example of position settings is shown bellow.
Now, if you want to set height and Width and Position of Tkinter window in one line code, then you have to use following syntax:
Lets see, how its work with given following example:
So, In This way, you can set the Size and Position of Tkinter easily.
Remember one thing, first you have to get the size of Screen by given root option.
winfo_screenwidth(): This will show the screen Width
winfo_screenheight(): This will show the screen Height
you can get more details of .geometry() from here.
Process (1):
Process (2):
Process (3):
Process (4):
So, all the process will work as same for center screen. You can choose anyone for your application development. The output will looks like:
These are the process for Set Tkinter Window Position or Center Screen Tkinter in Python with complete description.
So, here SKOTechLearn will explain and describe step by step process to Set Tkinter Window Position and Size or Center Screen Tkinter in Python.
Easy Way to Use Tkinter Label in Python.
Let’s Start with following way:
- Set Tkinter Window Size in Python
- Set Tkinter Window Position in Python
- Center Screen Tkinter Window in Python
By default tkinter is set in 200x200 height and width size.
(1). Set Tkinter Window Size in Python :
If you make an application in python using tkinter. And you want to increase or decrease the size of Window or Height width of your window, you have to use following syntax:Tk().geometry("widthxheight")
In this syntax, you have to change width and height with desired number or integer.
Now lets see the example how its work?
from tkinter import * TkForm = Tk() #Set Height and Width of Window TkForm.geometry("350x150") #Set the Title according to desire TkForm.title("TK Size in Python") TkForm.mainloop()When you run this code, the following geometry will show bellow
Tk Size Output |
Python If Else Statement with Details Description and Example.
(2). Set Tkinter Window Position in Python :
Like above given Size setting, you can set the position using “geometry” option in TK() class. So, let’s see the syntax of position setting.Tk().geometry("+Left+Top")
Here, you have to replace “Left” and “Top” with integer.
So, the simple example of position settings is shown bellow.
from tkinter import * TkForm = Tk() #position setting with desire location. # "+" sign indicate the Window's position in geometry option. TkForm.geometry("+120+50") TkForm.title("TK Position Setting") TkForm.mainloop()Now the output will be like this.
Location Setting |
Tk().geometry("widthxheight+Left+Top")
Lets see, how its work with given following example:
from tkinter import * TkForm = Tk() # Width and Height with Location Setting TkForm.geometry("350x150+220+80") TkForm.title("TK Position Setting") TkForm.mainloop()This will display the output like:
Height and Width with Location Setting |
(3). Center Screen Tkinter Window in Python :
Now, if you want to set widow as center Screen with desire screen Size, then we have found following way where you can set center screen in python.Remember one thing, first you have to get the size of Screen by given root option.
winfo_screenwidth(): This will show the screen Width
winfo_screenheight(): This will show the screen Height
you can get more details of .geometry() from here.
Process (1):
from Tkinter import * MyTkForm = Tk() #Set desired Tkinter Window Size. MyTkForm.geometry("350x150") #Same size will be defined in variable for center screen in Tk_Width and Tk_height Tk_Width = 350 Tk_Height = 150 #calculate coordination of screen and window form x_Left = int(MyTkForm.winfo_screenwidth()/2 - Tk_Width/2) y_Top = int(MyTkForm.winfo_screenheight()/2 - Tk_Height/2) # Write following format for center screen MyTkForm.geometry("+{}+{}".format(x_Left, y_Top)) MyTkForm.title("Process (1): Center screen with size") MyTkForm.mainloop()
Process (2):
from tkinter import * MyTkForm = Tk() # Set desired size (height and width) in following variable Tk_Width = 350 Tk_Height = 150 #calculate coordination of screen and window form positionRight = int( MyTkForm.winfo_screenwidth()/2 - Tk_Width/2 ) positionDown = int( MyTkForm.winfo_screenheight()/2 - Tk_Height/2 ) # Set window in center screen with following way. MyTkForm.geometry("{}x{}+{}+{}".format(500,200,positionRight, positionDown)) # Set Desired Title MyTkForm.title("Process (2) : Center screen with size") MyTkForm.mainloop()
Process (3):
from tkinter import * TkForm2 = Tk() #set center screen window with following coordination MyLeftPos = (TkForm2.winfo_screenwidth() - 500) / 2 myTopPos = (TkForm2.winfo_screenheight() - 300) / 2 TkForm2.geometry( "%dx%d+%d+%d" % (500, 300, MyLeftPos, myTopPos)) TkForm2.title("Process (3): TK Center Screen") TkForm2.mainloop()
Process (4):
import tkinter as tk TkForm3 = tk.Tk() # set window width and height mywidth = 350 myheight = 150 # get screen height and width scrwdth = TkForm3.winfo_screenwidth() scrhgt = TkForm3.winfo_screenheight() # write formula for center screen xLeft = (scrwdth/2) - (mywidth/2) yTop = (scrhgt/2) - (myheight/2) # set geometry TkForm3.geometry(str(mywidth) + "x" + str(myheight) + "+" + str(xLeft) + "+" + str(yTop)) TkForm3.title('Process (4): Center screen with size') TkForm3.mainloop()
So, all the process will work as same for center screen. You can choose anyone for your application development. The output will looks like:
Center Screen Window |
In process 3 what is the meaning of %dx%d+%d
ReplyDelete