728x90
반응형
SMALL

tkinter 를 이용한 마우스 이벤트는

window에 bind 함수를 이용하여 마우스 이벤트를 각각 연결시킨다.

 

이때 bind() 함수는 다음과 같이 두 개의 이자를 가져야 한다.

 - bind("이벤트명",연결함수) #이벤트 연결을 위한 가장 중요한 함수!!!!!

 

이벤트 종류는 다음과 같다.

<Button-1> : 마우스 왼쪽 버튼 클릭
<Button-2> : 마우스 중간 버튼 클릭
<Button-3> : 마우스 오른쪽 버튼 클릭
<Button-4> : 마우스 휠을 스크롤바에서 위로 올릴 때(scroll up event)
<Button-5> : 마우스 휠을 스크롤바에서 아래로 내릴 때(scroll down event)
<ButtonPress> : 아무 마우스 버튼이라도 눌리면 호출 된다. 휠까지도
<Double-Button-1> : 마우스 왼쪽 버튼 더블 클릭
<Double-Button-2> : 마우스 중간 버튼 더블 클릭
<Double-Button-3> : 마우스 오른쪽 버튼 더블 클릭
<Return> : 엔터가 눌러짐
<Key> : 키가 눌러짐
<Enter> : 마우스 포인터가 위젯에 들어올 때
<Leave> : 마우스 포인터가 위젯을 떠날때
<Configure> : 위젯 사이즈에 변화가 있을때

 

이중에서 마우스와 관련 된 이벤트를 적용한 코드 예제는 아래와 같다.

from tkinter import *

class MouseEvent:
    def __init__(self):
        window = Tk()
        window.title("마우스 이벤트")
        window.geometry("640x480")
        
        self.canvas=Canvas(window, bg ="white")
        self.canvas.pack(expand=True, fill=BOTH)
        window.bind("<Button-1>",self.MouseClickEvent)
        window.bind('<Double-1>', self.MouseDoubleClickEvent)
        window.bind('<B1-Motion>', self.MouseMotionEvent) 

        self.mousestr = "Mouse: "
        self.mouse_id = self.canvas.create_text(320,240,font="Times 15 italic bold",text=self.mousestr)    
        self.canvas.create_text(320,360,font="Times 15 italic bold",text="Mouse Event Example")
        
        while True:
            #
            self.canvas.itemconfigure(self.mouse_id, text=self.mousestr)
            #
            window.after(33)
            window.update()

    def MouseClickEvent(self, event):
        self.mousestr = "Mouse: " + str(event.x) + ", " + str(event.y) + "에서 마우스 클릭 이벤트 발생"
        

    def MouseDoubleClickEvent(self, event):
        self.mousestr = "Mouse: " + str(event.x) + ", " + str(event.y) + "에서 마우스 더블 클릭 이벤트 발생"

    def MouseMotionEvent(self, event):
        self.mousestr = "Mouse: " + str(event.x) + ", " + str(event.y) + "에서 마우스 모션 이벤트 발생"


MouseEvent()
728x90
반응형
LIST
728x90
반응형
SMALL

tkinter 를 이용한 마우스 이벤트는

window에 bind 함수를 이용하여 마우스 이벤트를 각각 연결시킨다.

 

이때 bind() 함수는 다음과 같이 두 개의 이자를 가져야 한다.

 - bind("이벤트명",연결함수) #이벤트 연결을 위한 가장 중요한 함수!!!!!

 

이벤트 종류는 다음과 같다.

<Button-1> : 마우스 왼쪽 버튼 클릭
<Button-2> : 마우스 중간 버튼 클릭
<Button-3> : 마우스 오른쪽 버튼 클릭
<Button-4> : 마우스 휠을 스크롤바에서 위로 올릴 때(scroll up event)
<Button-5> : 마우스 휠을 스크롤바에서 아래로 내릴 때(scroll down event)
<ButtonPress> : 아무 마우스 버튼이라도 눌리면 호출 된다. 휠까지도
<Double-Button-1> : 마우스 왼쪽 버튼 더블 클릭
<Double-Button-2> : 마우스 중간 버튼 더블 클릭
<Double-Button-3> : 마우스 오른쪽 버튼 더블 클릭
<Return> : 엔터가 눌러짐
<Key> : 키가 눌러짐
<Enter> : 마우스 포인터가 위젯에 들어올 때
<Leave> : 마우스 포인터가 위젯을 떠날때
<Configure> : 위젯 사이즈에 변화가 있을때

 

이중에서 마우스와 관련 된 이벤트를 적용한 코드 예제는 아래와 같다.

from tkinter import *

class MouseEvent:
    def __init__(self):
        window = Tk()
        window.title("마우스 이벤트")
        window.geometry("640x480")
        
        self.canvas=Canvas(window, bg ="white")
        self.canvas.pack(expand=True, fill=BOTH)
        window.bind("<Button-1>",self.MouseClickEvent)
        window.bind('<Double-1>', self.MouseDoubleClickEvent)
        window.bind('<B1-Motion>', self.MouseMotionEvent) 

        self.mousestr = "Mouse: "
        self.mouse_id = self.canvas.create_text(320,240,font="Times 15 italic bold",text=self.mousestr)    
        self.canvas.create_text(320,360,font="Times 15 italic bold",text="Mouse Event Example")
        
        while True:
            #
            self.canvas.itemconfigure(self.mouse_id, text=self.mousestr)
            #
            window.after(33)
            window.update()

    def MouseClickEvent(self, event):
        self.mousestr = "Mouse: " + str(event.x) + ", " + str(event.y) + "에서 마우스 클릭 이벤트 발생"
        

    def MouseDoubleClickEvent(self, event):
        self.mousestr = "Mouse: " + str(event.x) + ", " + str(event.y) + "에서 마우스 더블 클릭 이벤트 발생"

    def MouseMotionEvent(self, event):
        self.mousestr = "Mouse: " + str(event.x) + ", " + str(event.y) + "에서 마우스 모션 이벤트 발생"


MouseEvent()
728x90
반응형
LIST

+ Recent posts