Button
- tkinter.Button(윈도우 창, 속성1, 속성2, …)을 사용하여 버튼 속성을 설정
- 속성(매개변수) 중
command를 사용하여 사용자 정의함수를 실행할 수 있음
코드 예
root=Tk()
root.title("Label")
root.geometry("640x400+100+100")
root.resizable(0, 0)
count=0
def countup():
global count
count +=1
label.config(text=str(count))
label=Label(root, text="0")
label.pack()
button=Button(root, text="버튼을 누르세요", overrelief="solid", width=15, command=countup, repeatdelay=1000, repeatinterval=100)
button.pack()
root.mainloop()
객체.config() 함수는 객체의 속성을 변화시키는 메서드입니다. 위 함수 countup()에서 .config() 메서드는 label의 text 속성을 변경하기 위해 적용한 것입니다.
Button method
| 이름 | 의미 |
| invoke() | 버튼 실행 |
| flash() | 깜빡임 normal 상태의 색상과 active 상태의 색상이 사이에서 깜빡임 |
Button 문자열 설정
| 이름 | 의미 | 기본값 | 속성 |
| text | 버튼에 표시할 문자열 | - | - |
| textvariable | 버튼에 표시할 문자열을 가져올 변수 | - | - |
| anchor | 버튼안의 문자열 또는 이미지의 위치 | center | n, ne, e, se, s, sw, w, nw, center |
| justify | 버튼의 문자열이 여러 줄 일 경우 정렬 방법 | center | center, left, right |
| wraplength | 자동 줄내림 설정 너비 | 0 | 상수 |
Button 형태 설정
| 이름 | 의미 | 기본값 | 속성 |
| 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 | 상수 |
Button 형식 설정
| 이름 | 의미 | 기본값 | 속성 |
| 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
Button 상태 설정
| 이름 | 의미 | 기본값 | 속성 |
| state | 상태 설정 | normal | normal, active, disabled |
| activebackground | active 상태일 때 버튼의 배경 색상 | SystemButtonFace | color |
| activeforeground | active 상태일 때 버튼의 문자열 색상 | SystemButtonText | color |
| disabledforeground | disabeld 상태일 때 버튼의 문자열 색상 | SystemDisabledText | color |
Button 하이라이트 설정
| 이름 | 의미 | 기본값 | 속성 |
| highlightcolor | 버튼이 선택되었을 때 색상 | SystemWindowFrame | color |
| highlightbackground | 버튼이 선택되지 않았을 때 색상 | SystemButtonFace | color |
| highlightthickness | 버튼이 선택되었을 때 두께 (두께 설정) | 0 | 상수 |
Button 동작 설정
| 이름 | 의미 | 기본값 | 속성 |
| takefocus | Tab 키를 이용하여 위젯 이동 허용 여부 | True | Boolean |
| command | 버튼이 active 상태일 때 실행하는 메서드(함수) | - | 메서드, 함수 |
| repeatdelay | 버튼이 눌러진 상태에서 command 실행까지의 대기 시간 | 0 | 상수(ms) |
| repeatinterval | 버튼이 눌러진 상태에서 command 실행의 반복 시간 | 0 | 상수(ms) |
댓글
댓글 쓰기