Как я могу указать окно Tkinter, где можно открыть, на основе размеров экрана? Я хотел бы, чтобы он открывался посередине.
Как указать, где открывается окно Tkinter?
Ответ 1
Этот ответ основан на ответе Рейчел. Ее код не работал изначально, но с некоторой настройкой я смог исправить ошибки.
import tkinter as tk
root = tk.Tk() # create a Tk root window
w = 800 # width for the Tk root
h = 650 # height for the Tk root
# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.mainloop() # starts the mainloop
Ответ 2
Попробуйте это
import tkinter as tk
def center_window(width=300, height=200):
# get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# calculate position x and y coordinates
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root = tk.Tk()
center_window(500, 400)
root.mainloop()
Ответ 3
root.geometry('250x150+0+0')
Первые два параметра - это ширина и высота окна. Последние два параметра - это координаты экрана x и y. Вы можете указать необходимые координаты X и Y
Ответ 4
root.geometry('520x400+350+200')
Пояснение: ('ширина x высота + координата X + координата Y')