宝哥软件园

python tkinter 窗口居中对齐

编辑:宝哥软件园 来源:互联网 时间:2020-03-15

tkinter没有现成的窗口居中的功能,只能间接地算出来。

from Tkinter import *
def center_window(w=300, h=200):
    # get screen width and height
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    # calculate position x, y
    x = (ws/2) - (w/2)    
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root = Tk() 
center_window(500, 300) 
root.mainloop()
更多资讯
游戏推荐
更多+