2013-02-16 18 views

Odpowiedz

46

Ta odpowiedź jest oparta na Rachel's answer. Jej kod początkowo nie działał, ale po kilku poprawkach udało mi się naprawić błędy.

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 
21

Spróbuj

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() 

Source

+0

lub odnieść się do http://eurion.net/python-snippets/snippet/Center % 20window.html dla alternatywnej metody –

+1

dla maksymalnej użyteczności, możesz chcieć poprawić wcięcie. – mgilson

+1

@RachelGallen: czy zdajesz sobie sprawę, że kod jest dla zupełnie innego zestawu narzędzi? Nie możesz użyć pyqt do wyśrodkowania okna tkintera. –

5
root.geometry('250x150+0+0') 

użytku to pierwsze dwa parametry są szerokość i wysokość okna. Ostatnie dwa parametry to współrzędne ekranu X i Y. Możesz podać wymagane współrzędne X i Y:

Powiązane problemy