1 | initial version |
You can use the tkinter
library in Python to position windows. Here is an example code that positions a window at the center of the screen:
import tkinter as tk
root = tk.Tk()
# get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# calculate x and y position for the window
x = (screen_width/2) - (300/2) # width of the window = 300
y = (screen_height/2) - (200/2) # height of the window = 200
# set the position of the window
root.geometry('%dx%d+%d+%d' % (300, 200, x, y))
root.mainloop()
The root.geometry()
method takes four parameters: width, height, x position, and y position. The %dx%d+%d+%d
format string is used to pass these parameters to the method. The x
and y
positions are calculated by subtracting half of the window size from the width and height of the screen respectively.