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 6. Button ( 버튼 )
PYTHON/tkinter

(Python Tkinter) Chapter 6. Button ( 버튼 )

2022. 9. 13. 11:40
728x90
반응형

"컨트롤의 세계에서 가장 본질적인 요소는 버튼이다.

버튼은 수없이 많은 얼굴을 지녔지만 실상은 하나다."

 

엘런 쿠퍼는 그의 저서 <About Face 4>에서 위와 같이 언급했습니다. 버튼은 UI의 가장 기본적인 요소이자 핵심적인 요소입니다. 버튼을 누르는 순간 입력 데이터가 전송되거나 명령을 실행하는 상호작용이 이루어지며 사용 상황이 크게 바뀌게 됩니다. 오늘은 Button의 간단한 사용법과 활용 예에 대해 알아보도록 하겠습니다.  

 


 

Button ( 버튼 )

 

 

 

 

 

 

Button은 사용자가 데이터를 전송하거나 어떠한 기능을 실행하도록 하는 위젯입니다.

메서드 또는 함수 등을 실행시키기 위한 Button을 생성할 수 있습니다.

 


 

Syntax ( 구문 )

 

Button 위젯을 사용하려면 다음 구문을 사용합니다.

 

button = tk.Button(container, **option)

 

  • container - parent window

 

아래는 Button을 설명하기 위한 Skeleton Code 입니다.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('800x600')
root.resizable(False, False)
root.title('Py-Life Widget Demo')

# show the widget syntax here

root.mainloop()

 

 


 

Option Parameters

 

Button 위젯에는 모양을 사용자 정의할 수 있는 많은 옵션이 있습니다.

 

 

- 문자열 설정 -

Name Meaning Default Preference
text 버튼에 표시할 문자열 - -
textvariable 버튼에 표시할 문자열을 가져올 변수 - -
anchor 버튼안의 문자열 또는 이미지의 위치 center n, ne, e, se, s, sw, w, nw, center
justify 버튼의 문자열이 여러 줄 일 경우 정렬 방법 center center, left, right
wraplength 자동 줄내림 설정 너비 0 상수

 

 

- Widget Form -

 

Name Meaning Default Preference
width 너비 0 상수
height 높이 0 상수
relief 테두리 모양 flat flat, groove, raised, ridge,
solid, sunken
overrelief     마우스를 올렸을 때 테두리 모양       raised       flat, groove, raised, ridge,
solid, sunken
borderwidth | bd 테두리 두께 2 상수
background | bg 배경 색상       SystemButtonFace color                
foreground | fg 문자열 색상         SystemButtonFace color                  
padx 테두리와 내용의 가로 여백 1 상수
pady 테두리와 내용의 세로 여백 1 상수
bitmap 포함할 기본 이미지 - info, warning, error, question,
questhead, hourglass, gray12,
gray25, gray50, gray75
image 포함할 임의 이미지 - -
compound 문자열과 이미지 동시
표시 시 이미지 위치
none bottom, center, left, none,
right, top
font 문자열 글꼴 설정 TkDefaultFont font
cursor*** 마우스 커서 모양 - 커서 속성***

 

*** Cursor 매개 변수

arrow, based_arrow_down, based_arrow_up, boat, bogosity, bottom_left_corner, bottom_right_corner, bottom_side, bottom_tee, box_spiral, center_ptr, circle, clock, coffee_mug, cross, cross_reverse, crosshair, diamond_cross, dot, dotbox, double_arrow, draft_large, draft_small, draped_box, exchange, fleur, gobbler, gumby, hand1, hand2, heart, icon, iron_cross, left_ptr, left_side, left_tee, leftbutton, ll_angle, lr_angle, man, middlebutton, mouse, pencil, pirate, plus, question_arrow, right_ptr, right_side, right_tee, rightbutton, rtl_logo, sailboat, sb_down_arrow, sb_h_double_arrow, sb_left_arrow, sb_right_arrow, sb_up_arrow, sb_v_double_arrow, shuttle, sizing, spider, spraycan, star, target, tcross, top_left_arrow, top_left_corner, top_right_corner, top_side, top_tee, trek, ul_angle, umbrella, ur_angle, watch, wait, xterm, X_cursor

 

 

- Widget State -

Name Meaning Default Preference
state 상태 설정 normal normal***, active, disabled
activebackground active 상태일 때 버튼의 배경 색상 SystemButtonFace color
activeforeground active 상태일 때 버튼의 문자열 색상 SystemButtonText color
disabledforeground disabeld 상태일 때 버튼의 문자열 색상 SystemDisabledText color

*** 기본 설정은 normal 상태의 설정을 의미합니다. (bg, fg 등의 설정)

 

 

- Highlight Options -

Name Meaning Default Preference
highlightcolor 버튼이 선택되었을 때 색상 SystemWindowFrame color
highlightbackground 버튼이 선택되지 않았을 때 색상 SystemButtonFace color
highlightthickness 버튼이 선택되었을 때 두께*** 0 상수

*** highlightbackground를 설정하였을 경우, 버튼이 선택되지 않았을 때에도 두께가 표시됩니다.

 

 

- 버튼 동작 설정 -

Name Meaning Default Preference
takefocus Tab 키를 이용하여 위젯 이동 허용 여부 True Boolean
command 버튼이 active 상태일 때 실행하는 메서드(함수) - 메서드, 함수
repeatdelay 버튼이 눌러진 상태에서 command 실행까지의 대기 시간 0 상수(ms)
repeatinterval 버튼이 눌러진 상태에서 command 실행의 반복 시간 0 상수(ms)

*** repeatdelay=100 일 경우, 누르고 있기 시작한 0.1초 후에 command가 실행됨

*** repeatdelay=1000, repeatinterval=100 일 경우, 1초 후에 command가 실행되며 0.1초마다 버튼을 뗄 때까지 command가 계속 실행

*** repeatinterval 매개변수는 repeatdelay 매개변수와 같이 사용해야 함

 

 


 

Button의 활용

 

1. Basic

 

( Input )

 

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('800x600')
root.resizable(False, False)
root.title('Py-Life Widget Demo')

# show the widget syntax here >>>

## Create Button Widget
btn_1 = tk.Button(root, text = 'BTN 1', bg = 'yellow', fg = 'red')

## Option Setting
btn_1.config(width = 10, height = 3)
btn_1.config(text = 'Button 1')

## Place the Button
btn_1.pack()

root.mainloop()

 

 

( Output )

 

 

 

버튼의 tkinter event 처리( command / bind ) 관련 내용은 아래 링크 참고 바랍니다.


https://pylife.tistory.com/entry/tkinter-button-command-bind

 

(5min. Python) tkinter event 처리 ( command / bind )

"Life is too short, You need python" command 및 bind method는 모두 Python의 tkinter 모듈을 사용하여 버튼 등 위젯에 생명과 기능을 추가하여 이벤트 처리를 하는 데 사용됩니다. UI의 수준이 기초 수준을..

pylife.tistory.com

 


 

마무리

 

Tkinter Button의 사용법과 다양한 활용 방법에 대해 알아보았습니다. Chapter 7에서는 Entry의 사용법과 다양한 활용 방법에 대해 알아보도록 하겠습니다.

 

 

 

 

 

 

 

 

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

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

(Tkinter - Basic) Chapter 8. Frame (프레임)  (0) 2022.09.16
(Python Tkinter) Chapter 7. Entry ( 엔트리 )  (0) 2022.09.14
(Python Tkinter) Chapter 5. Label ( 레이블 )  (0) 2022.09.08
(Python Tkinter) Chapter 4. place - Geometry(Layout) Managers  (0) 2022.07.28
(Python Tkinter) Chapter 3. grid - Geometry(Layout) Managers  (0) 2022.07.28
    Creatio ex nihilo
    Creatio ex nihilo
    "Life is too short, You need python"

    티스토리툴바