Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Tkinter 창에서 버튼을 어떻게 배치합니까?

<시간/>

버튼의 위치를 ​​설정하기 위해 버튼 위젯의 place 메소드를 사용합니다. place 메소드는 버튼의 x 및 y 좌표를 사용합니다.

단계 -

  • 필요한 라이브러리를 가져오고 tkinter 프레임의 인스턴스를 만듭니다.

  • win.geometry를 사용하여 프레임 크기를 설정합니다. 방법.

  • 다음으로 여러 개의 버튼을 만들고 이름을 "Button-1", "Button-2" 등으로 지정합니다.

  • x 및 y 좌표 값을 제공하여 place 방법을 사용하여 버튼의 위치를 ​​설정합니다.

  • 마지막으로 메인 루프를 실행합니다. 응용 프로그램 창의.

예시

# Import the Tkinter library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter frame
win = Tk()

# Define the geometry
win.geometry("750x350")

# Create Buttons in the frame
button = ttk.Button(win, text="Button-1")
button.place(x=325, y=125)

button = ttk.Button(win, text="Button-2")
button.place(x=325, y=175)

button = ttk.Button(win, text="Button-3")
button.place(x=325, y=225)

#Create a Label
Label(win, text="Position the Buttons", font='Consolas 15').pack()

win.mainloop()

출력

이 코드를 실행하면 다음 창이 표시됩니다 -

Tkinter 창에서 버튼을 어떻게 배치합니까?

x 3개의 버튼 모두에서 325로 변수가 지정되므로 버튼이 정렬됩니다. (x, y)를 변경할 수 있습니다. 버튼의 위치를 ​​변경하려면 place 메소드에 값을 입력하십시오.