Python tkinter Bind (Event 연결)

Python tkinter Bind 함수에 대해 살펴보겠습니다.

Bind함수로 위젯들에 Event 별로 실행할 함수를 설정할 수 있습니다.

 

Bind 사용하기

import tkinter

win = tkinter.Tk();

def func1(event):
	print("func1 - leftMouseClick");

def func2(event):
	print("func2 - rightMouseClick");

label = tkinter.Label(win, text="click this");
label.pack();
label.bind("<Button-1>", func1);
label.bind("<Button-2>", func2);

win.mainloop();

위 코드는 라벨에 왼쪽 마우스 클릭과 오른쪽 마우스 클릭 시에 각각 함수를 실행하도록

bind함수로 이벤트와 함수를 연결했습니다.

 

이런식으로 위젯들에 이벤트가 발생했을 때 함수들이 실행되도록 연결하여

이벤트별로 실행합니다.

 

Event 종류

분류 이름 기능
Button <Button-1> 마우스 왼쪽버튼 누를 때
<Button-2> 마우스 휠 누를 때
<Button-3> 마우스 오른쪽버튼 누를 때
<Button-4> 스크롤업
<Button-5> 스크롤다운
<MouseWheel> 마우스 휠 이동
Motion <Motion> 마우스 움직일 때
<B1-Motion> 마우스 왼쪽버튼 누른상태로 움직일 때
<B2-Motion> 마우스 휠 누른상태로 움직일 때
<B3-Motion> 마우스 오른쪽버튼 누른 상태로 움직일 때
Release <ButtonRelease-1> 마우스 왼쪽버튼 뗄 때
<ButtonRelease-2> 마우슨 휠 버튼 뗄 때
<ButtonRelease-3> 마우스 오른쪽버튼 뗄 때
DoubleClick <Double-Button-1> 마우스 왼쪽버튼 더블 클릭할 때
<Double-Button-2> 마우스 휠버튼 더블 클릭할 때
<Double-Button-3> 마우스 오른쪽버튼 더블 클릭할 때
WidgetOperation <Enter> 위젯안으로 마우스포인터 진입시
<Leave> 위젯밖으로 마우스포인터 나갈시
<FocusIn> Tab키로 위젯선택시
<FocusOut> Tab키로 위젯선택풀릴시
<Configure> 위젯 모양 수정시
KeyInput <Key> 특정 키가 입력되었을 때
<A>, <3>, <F2> 등...
<Return> Enter키 입력시
<Cancel> Break키 입력시
<BackSpace> BackSpace키 입력시
<Caps_Lock> CapsLock키 입력시
<Prior> Page Up키 입력시
<Up>, <Down>, <Right>, <Left> 방향키 입력시
Assistant KeyInput <Shift-Key> Shift + 특정 Key 입력시
<Control-Key> Ctrl + 특정 Key 입력시
<Alt-Key> Alt + 특정 Key 입력시