Ask Your Question
2

How can I position windows with Python programming?

asked 2023-07-16 11:08:31 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-16 11:12:01 +0000

woof gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-16 11:08:31 +0000

Seen: 10 times

Last updated: Jul 16 '23