How to specify where a Tkinter window opens?

后端 未结 5 1707
抹茶落季
抹茶落季 2020-12-02 12:54

How can I tell a Tkinter window where to open, based on screen dimensions? I would like it to open in the middle.

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 13:27

    Try this

    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

提交回复
热议问题