본문 바로가기
Python/GUI

[Python/GUI] 배치 관리자

by 기록하는_사람 2022. 10. 14.

배치 관리자

📌 배치 관리자

: 화면에서 위젯의 배치를 담당하는 객체.

  ① 압축(pack) 배치 관리자

  ② 격자(grid) 배치 관리자

  ③ 절대(place) 배치 관리자

 

격자(grid) 배치 관리자

📌 격자(grid) 배치 관리자

: 위젯을 테이블 형태로 배치.

  row, column으로 분할하며, 크기는 자동으로 설정됨. 

 

📄 섭씨 ↔ 화씨 변경해주는 프로그램.

from tkinter import *

def f_to_c():
    f = float(e1.get().strip())  # .get() : e1에 입력된 값 가져옴. 
    e2.insert(0, str((f - 32) * 5 / 9))  # .insert() : e2에 값 입력. 

def c_to_f():
    c = float(e2.get())  # .get() : e2에 입력된 값 가져옴. 
    e1.insert(0, str((c * 1.8) + 32))  # .insert() : e1에 값 입력. 

window = Tk()

l1 = Label(window, text="화씨")
l2 = Label(window, text="섭씨")
l1.grid(row=0, column=0)  # grid 배치 
l2.grid(row=1, column=0)  # grid 배치 

e1 = Entry(window)  # 화씨
e2 = Entry(window)  # 섭씨
e1.grid(row=0, column=1)  # grid 배치 
e2.grid(row=1, column=1)  # grid 배치 

b1 = Button(window, text="화씨  -> 섭씨", command=f_to_c)
b2 = Button(window, text="섭씨 -> 화씨", command=c_to_f)
b1.grid(row=2, column=0)  # grid 배치 
b2.grid(row=2, column=1)  # grid 배치 

window.mainloop()

 

절대(place) 배치 관리자

📌 절대(place) 배치 관리자

: x, y 입력해 배치. 

 

📄 섭씨 ↔ 화씨 변경해주는 프로그램.

from tkinter import *

def f_to_c():
    f = float(e1.get().strip())  # .get() : e1에 입력된 값 가져옴. 
    e2.insert(0, str((f - 32) * 5 / 9))  # .insert() : e2에 값 입력. 

def c_to_f():
    c = float(e2.get())  # .get() : e2에 입력된 값 가져옴. 
    e1.insert(0, str((c * 1.8) + 32))  # .insert() : e1에 값 입력. 

window = Tk()
window.title("절대(place) 배치 관리자")  # 타이틀바 텍스트 설정. 
window.geometry("300x250")  # 창 크기 설정.

l1 = Label(window, text="화씨")
l2 = Label(window, text="섭씨")
l1.place(x=50, y=50)  # place 배치 
l2.place(x=50, y=100)  # place 배치 

e1 = Entry(window)  # 화씨
e2 = Entry(window)  # 섭씨
e1.place(x=100, y=50)  # place 배치 
e2.place(x=100, y=100)  # place 배치 

b1 = Button(window, text="화씨  -> 섭씨", command=f_to_c)
b2 = Button(window, text="섭씨 -> 화씨", command=c_to_f)
b1.place(x=50, y=150)  # place 배치 
b2.place(x=160, y=150)  # place 배치 

window.mainloop()

'Python > GUI' 카테고리의 다른 글

[Python/GUI] 이미지  (0) 2022.10.14
[Python] Button, Label, Entry  (0) 2022.10.14
[Python/GUI] tkinter  (0) 2022.10.14

댓글