250x250
250x250
  • 분류 전체보기 (48)
    • Save Your TIME (1)
      • WORK SMART - RPA (0)
      • 비 전공 직장인의 업무 자동화 고군분투기 (1)
    • Make a lot of MONEY (0)
      • 부동산 (0)
    • PYTHON (45)
      • BASIC (3)
      • pandas (3)
      • tkinter (8)
      • LIBRARY | ETC (1)
      • Tips (19)
      • Error Solutions (11)
    • EXCEL (0)
      • BASIC (0)
      • TIP (0)
    • Life & Education (2)
      • 초등 수학 (2)
      • English (0)
    • TISTORY (0)
      • ABOUT BLOG (0)
      • TIP (0)
    • Reference (0)
hELLO · Designed By 정상우.
Creatio ex nihilo

Py Life

(Python Tkinter) Chapter 4. place - Geometry(Layout) Managers
PYTHON/tkinter

(Python Tkinter) Chapter 4. place - Geometry(Layout) Managers

2022. 7. 28. 14:34
728x90
반응형

"Life is too short, You need python"

Place Geometry Manager는 위젯을 특정 위치에 배치하는 방법이며 Tkinter에서 제공되는 세 가지 Geometry Manager 중 가장 단순하며 직관적입니다. (x, y) 좌표계를 사용하며 위치와 크기를 픽셀 단위로 지정하며 지정 위치에 위젯을 정확하게 배치할 수 있습니다.  또한 창의 크기를 조정해도 위젯의 크기와 위치는 변경되지 않습니다. Place를 사용하여 창에 위젯을 배치하는 방법에 대해 알아보겠습니다.

 


 

place ( 좌표 위치 배치 ) 

 

place는 (x, y) 좌표계에 따라 지정된(절대 or 상대) 위치에 위젯을 배치합니다.

 

(x, y) 좌표에 따라 배치

 


 

Syntax

 

widget.place(option parameters)

 

  • 우선 순위는 가장 처음 선언한 place 부터 배치 됩니다.
  • place 의 x =, y = 옵션은 단위가 픽셀이며 위젯의 절대 위치 입니다. 

      → button_1.place(x = 20, y = 20)는 button_1이 (20, 20)의 좌표에 배치됨을 의미 합니다.

      → 크기를 조정할 수 있습니다.

  • 상대 위치가 있는 창에 다른 위젯이 있는 경우 창 확대 / 축소 시 다른 위젯과 겹칠 수 있습니다.
  • place는 pack / place와 함께 사용할 수 있습니다.

 

 

( Input )

 

import tkinter as tk

root = tk.Tk()
root.title('PY Life')
root.geometry("500x400")

Button_1 = tk.Button(root, text = '(50, 50)', bg = 'green')
Button_2 = tk.Button(root, text = '(50, 100)', bg = 'red')
Button_3 = tk.Button(root, text = '(100, 150)', bg = 'blue')
Button_4 = tk.Button(root, text = '(100, 200)', bg = 'gray')
Button_5 = tk.Button(root, text = '(0, 300)', bg = 'yellow')
Button_6 = tk.Button(root, text = '(100, 300)', bg = 'pink')

Button_1.place(x = 50, y = 50)
Button_2.place(x = 50, y = 100)
Button_3.place(x = 100, y = 150)
Button_4.place(x = 100, y = 200)
Button_5.place(x = 0, y = 250)
Button_6.place(x = 0, y = 300)

root.mainloop()

 

( Output )

 

 

별다른 옵션이 없다면 위의 그림과 같이 출력이 됩니다.

 


 

Option Parameters

 

place()에 옵션을 추가해야 원하는 형태로 배치가 가능하며 가능한 옵션은 다음과 같습니다. 

 

이름
의미 기본값 속성
x x좌표 배치 0 상수(pixel 단위)
y y좌표 배치 0 상수(pixel 단위)
relx x좌표 배치 비율(상위 위젯 기준) 0 0 ~ 1
rely y좌표 배치 비율(상위 위젯 기준) 0 0 ~ 1
width 위젯의 너비 0 상수(pixel 단위)
height 위젯의 높이 0 상수(pixel 단위)
relwidth 위젯의 너비 비율(상위 위젯 기준) 0 0 ~ 1 (0 ~ 100%)
relheight 위젯의 높이 비율(상위 위젯 기준) 0 0 ~ 1 (0 ~ 100%)
anchor 위젯의 기준 위치 nw n, s, e, w, nw, ne, es, ws, ns, ew, news
bordermode 상위 위젯의 테두리 참조    

 


 

Option 1 . x / y / relx / rely / width / height / relwidth / relheight

 

place()는 (x, y), (relx, rely)로 좌표를 받아 위젯를 배치하고, width / height, relwidth / relheight로 크기를 조정합니다.

  • x = (pixel), y = (pixel) : 해당 좌표에 위젯을 배치합니다.
  • relx = (비율), rely = (비율) : 비율은 0 ~ 1 사이 값으로 상위 위젯 사이즈를 기준으로 합니다.
  • width = (pixel), height = (pixel) : 주어진 Pixel 값에 따라 사이즈를 결정합니다.
  • relwidth = (비율), width = (비율) : 비율은 0 ~ 1 사이 값으로 상위 위젯 사이즈를 기준으로 합니다.

 

아래의 예시를 통해 사용 방법을 확인해 보도록 하겠습니다.

 

 

( Input )

 

import tkinter as tk

root = tk.Tk()
root.title('PY Life')
root.geometry("600x500")

Button_1 = tk.Button(root, text = '100px, 200px', bg = 'yellow')
Button_2 = tk.Button(root, text = '200px, 50px / 200px_width', bg = 'green')
Button_3 = tk.Button(root, text = '250px, 100px / 40px_height', bg = 'red')
Button_4 = tk.Button(root, text = '350px, 200px / 50px high', bg = 'purple')
Button_5 = tk.Button(root, text = '350px, 300px / 200px width', bg = 'orange')
Button_6 = tk.Button(root, text = '100px, 300px / relw 0.4, relh 0.2', bg = 'pink')
Button_7 = tk.Button(root, text = '0, 0 / Relh 0.6', bg = 'cyan')
Button_8 = tk.Button(root, text = '150px, 0 / Relw 0.2', bg = 'royalblue')
Button_9 = tk.Button(root, text = 'Relx 0.7, 0', bg = 'beige')
Button_10 = tk.Button(root, text = '0, Rely 0.8', bg = 'brown')

Button_1.place(x = 100, y = 200)
Button_2.place(x = 200, y = 50, width = 200)
Button_3.place(x = 250, y = 100, height = 40)
Button_4.place(x = 350, y = 200, height = 50)
Button_5.place(x = 350, y = 300, width = 200)
Button_6.place(x = 100, y = 300, relwidth = 0.4, relheight = 0.2)
Button_7.place(relheight = 0.6)
Button_8.place(x = 150, relwidth = 0.4)
Button_9.place(relx = 0.7)
Button_10.place(rely = 0.8)

root.mainloop()

각 셀별 Color를 추가하고 구분 하였습니다.

 

( Output )

 


 

Option 2. anchor

 

anchor는 (x, y) 좌표가 위젯의 어디에 위치할 것인지

 

 

( Input )

 

Button_1 = tk.Button(root, text = 'nw', bg = 'yellow')
Button_2 = tk.Button(root, text = 'ne', bg = 'blue')
Button_3 = tk.Button(root, text = 'sw', bg = 'purple')
Button_4 = tk.Button(root, text = 'se', bg = 'cyan')

Button_1.place(x = 200, y = 200, anchor = 'nw')
Button_2.place(x = 200, y = 200, anchor = 'ne')
Button_3.place(x = 200, y = 200, anchor = 'sw')
Button_4.place(x = 200, y = 200, anchor = 'se')

 

( Output )

 

 


 

마무리

 

Geometry Manager 중 place 사용법에 대해 알아보았습니다.

728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

'PYTHON > tkinter' 카테고리의 다른 글

(Python Tkinter) Chapter 6. Button ( 버튼 )  (0) 2022.09.13
(Python Tkinter) Chapter 5. Label ( 레이블 )  (0) 2022.09.08
(Python Tkinter) Chapter 3. grid - Geometry(Layout) Managers  (0) 2022.07.28
(Python Tkinter) Chapter 2. pack - Geometry(Layout) Managers  (0) 2022.07.21
(Python Tkinter) Chapter 1. GUI 생성  (0) 2022.06.25
    Creatio ex nihilo
    Creatio ex nihilo
    "Life is too short, You need python"

    티스토리툴바