Tkinter window position
Tkinter window position setting, geometry( ) method can not only set the size of the window, but also set the position of the window, at this time it has the following syntax format.
geometry(widthxheight+x+y)
-
The widthxheight above has been stated as the width and height of the window, with width and height separated by
x
. -
+x
means x is the distance from the left side of the window to the left side of the screen, if it is-x
, it means x is the distance from the right side of the window to the right side of the screen. -
+y
means y is the distance from the top side of the window to the top side of the screen, and if it is-y
, it means y is the distance from the bottom side of the window to the bottom side of the screen.
Example#1
Create a window of size 300×160, the coordinates of the upper left corner of this window are (400,200).
'''
Set the properties of the window
'''
from tkinter import *
# import tkinter
root = Tk() # Create root window Customized Tk object name, can also take other names
root.title("apidemos.com window") # Title of the window
# root.geometry("300x160") # The size of the window
# root.configure(bg='#00ff00') # Background color of the window
# root.configure(bg='Olive') # Background color of the window
# root.configure(bg='#808000') # Background color of the window
# root.iconbitmap("mystar.ico")
# root.iconbitmap("snake.ico") # Normal display
# root.iconphoto(False, PhotoImage(file='1.png')) # Can display png images normally
# root.iconphoto(False, PhotoImage(file='1.jpg')) # Program error reported: _tkinter.TclError: couldn't recognize data in image file "1.jpg"
root.geometry("300x160+400+200") # Indicates the distance from the top left corner of the screen (400,200)
# +x means the distance of the left side of the window from the left side of the screen, -x means the distance of the right side of the window from the right side of the screen
# +y has a similar meaning to -y, the distance of the upper (lower) side of the window from the upper (lower) side of the screen
root.mainloop() # Let the program continue to run while entering the wait and handle window event, placed on the last line of the program
Output:
Example#2
Redesign the geometry( ) method so that the reader can decide which way to use to create the window and set the window position.
'''
Set the properties of the window
'''
from tkinter import *
# import tkinter
root = Tk() # Create root window Customized Tk object name, can also take other names
w = 300 # Window width
h = 160 # Window height
x = 400 # The x-axis position in the upper left corner of the window
y = 200 # The position of the y-axis in the upper left corner of the window
root.geometry("%dx%d+%d+%d" % (w,h,x,y)) # Indicates the distance from the top left corner of the screen (400,200)
# +x means the distance of the left side of the window from the left side of the screen, -x means the distance of the right side of the window from the right side of the screen
# +y has a similar meaning to -y, the distance of the upper (lower) side of the window from the upper (lower) side of the screen
root.mainloop() # Let the program continue to run while entering the wait and handle window event, placed on the last line of the program
Output:
The following methods can be used in the tkinter module to obtain the width and height of the screen.
winfo_screenwidth() #Screen width
winfo_screenheight() #Screen height
Example#3
Design window while placing this window in the center of the screen.
'''
Set the properties of the window
'''
from tkinter import *
# import tkinter
root = Tk() # Create root window Customized Tk object name, can also take other names
screenWidth = root.winfo_screenwidth() # Screen width
screenHeight = root.winfo_screenheight() # Screen height
print("# screenWidth:",screenWidth) # Screen width: 1641
print("# screenHeight:",screenHeight) # Screen height: 888
w = 300 # Window width
h = 160 # Window height
x = (screenWidth - w)/2 # The x-axis position in the upper left corner of the window
y = (screenHeight - h)/2 # The position of the y-axis in the upper left corner of the window
root.geometry("%dx%d+%d+%d" % (w,h,x,y)) # Indicates the distance from the top left corner of the screen (400,200)
# +x means the distance of the left side of the window from the left side of the screen, -x means the distance of the right side of the window from the right side of the screen
# +y has a similar meaning to -y, the distance of the upper (lower) side of the window from the upper (lower) side of the screen
root.mainloop() # Let the program continue to run while entering the wait and handle window event, placed on the last line of the program
Output: