복붙노트

[PYTHON] Tkinter 창이 열리는 위치를 지정하는 방법?

PYTHON

Tkinter 창이 열리는 위치를 지정하는 방법?

화면 크기에 따라 Tkinter 창을 어디에서 열 것인지 어떻게 알 수 있습니까? 중간에 열어보고 싶습니다.

해결법

  1. ==============================

    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. ==============================

    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. ==============================

    3.

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

    첫 번째 두 매개 변수는 창의 너비와 높이입니다. 마지막 두 매개 변수는 x 및 y 화면 좌표입니다. 필요한 x 및 y 좌표를 지정할 수 있습니다.

  4. from https://stackoverflow.com/questions/14910858/how-to-specify-where-a-tkinter-window-opens by cc-by-sa and MIT license