본문 바로가기
Python/Tkinter

Button

by memora 2024. 10. 4.

 【기  능】버튼(Button)은 시작과 종료등과 같은 특정의 처리를 지시하는 트리거의 역할을 한다.


 【서  식】  
   handle tkinter.Button(parent window handle, option1, option2, option3, ...)
 
 【설  명】  
   parent window 상에 클릭 가능한 버튼을 옵션이 지정한 형태로 표시한다.
 
 【메소드】  

메소드 명칭 기능
invoke() 버튼 실행
flash() 버튼을 깜박거리게 한다

 

 

 【옵  션】  

 

1) 문자열 관련 옵션

옵션 명 기능 초기값 설명/속성(예)
text Button에 표시할 문자열 -  
textvariable Button에 표시할 문자열 변수 -  
anchor Button안의 문자열 또는 이미지의 위치 center n, ne, e, se, s, sw, w, nw, center
justify Button안의 문자열이 여러 줄 일 경우 정렬 방법 center center, left, right
wraplength 자동 줄바꾸기 설정 너비   상수(pixel단위로 설정)

 

 

2) Button의 형태 관련 옵션

옵션 명 기능 초기값 설명/속성(예)
width Button의 너비 0 상수
height Button의 높이 0 상수
relief Button의 테두리 모양 flat flat, groove, raised, ridge, solid, sunken
overrelief Button에 마우스를 올렸을 때 버튼의 테두리 모양 raised flat, groove, raised, ridge, solid, sunken
borderwidth=bd Button의 테두리 두께 2  
background=bg Button의 배경 색상 SystemButtonFace color
foreground=fg Button의 문자열 색상 SystemButtonFace color
padx Button의 테두리와 내용의 가로 여백 1 상수
pady Button의 테두리와 내용의 세로 여백 1 상수

 

 

3) Button의 형식 관련 옵션

옵션 명 기능 초기값 설명/속성(예)
bitmap Button에 포함할 기본 이미지 - info, warning, error, question, questhead, hourglass, gray12, gray25, gray50, gray75
image Button에 포함할 임의 이미지 - -
compound Button에 문자열과 이미지를 동시에 표시할 때 이미지의 위치 none bottom, center, left, none, right, top
font Button의 문자열 글꼴 설정 TkDefaultFont font
cursor Button의 마우스 커서 모양 - 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

 

 

4) Button의 상태 관련 옵션

옵션 명 기능 초기값 설명/속성(예)
state 상태 설정 normal normal, active, disabled
activebackground active 상태시의 배경 색상 SystemButtonFace color
activeforeground active 상태시의 문자열 색상 SystemButtonText color
disabledforeground disabeld 상태시의 문자열 색상 SystemDisabledText color

 

 

5) Button의 선택 관련 옵션

옵션 명 기능 초기값 설명/속성(예)
highlightcolor select시의 색상 SystemWindowFrame color
highlightbackground non select시의 색상 SystemButtonFace color(설정시 두께 상시표시)
highlightthickness select시의 두께 0

 

 

6) Button의 동작 관련 옵션

옵션 명 기능 초기값 설명/속성(예)
takefocus Tab 키에 의한 위젯 이동 허용 여부 True Boolean
command active시 실행하는 메소드 (함수) - 메소드 실행
repeatdelay 클릭후 command 실행 대기 시간 0 상수(ms)
repeatinterval 클릭상태 유지시 command 반복 시간 0 상수(ms), 단독사용 불가,
repeatdelay와 동시 설정필요 

 

 

【리턴 값】

실패 : none

성공 : Button의 핸들( .!button, .!button2등의 문자열 핸들)

 

【사용 예】

1) Button의 표시

import tkinter as tk

root = tk.Tk()
root.title("Button Options")
root.geometry( '250x150' )

button1 = tk.Button(text = u'Hello Button')
button1.place(x = 50, y = 20)

root.mainloop()

 

Button의 표시

 

2) Button의 응용

import tkinter as tk

root = tk.Tk()
root.title('Tkinter Button')
root.geometry('250x150')

button1 = tk.Button(text = u'Hello Button', font = ('MS Gothic', 20), background = '#FFDFDF' )
button1.place(x = 10, y = 10, width = 230, height = 50)

root.mainloop()

 

응용 버튼의 표시

 

3) Button의 변화/동작

import tkinter as tk

count = 1

def func(event):
    global count
    count += 1
    button1['text'] = str(count)

root = tk.Tk()
root.title('Tkinter Button')
root.geometry('250x150')

button1 = tk.Button(text = str( count ), font = ('MS Gothic', 20), background = '#FFDFDF')
button1.place(x = 10, y = 10, width = 230, height = 50)

button1.bind('<Button-1>', func)

root.mainloop()

변화하는 버튼의 표시1(클릭시)

 

변화하는 버튼의 표시2(클릭시)

 

반응형

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

Label  (2) 2024.09.29
Tkinter Introduction  (0) 2024.09.26