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
728x90
반응형
SMALL

아래 이미지, 사운드(효과음) 파일을 다운로드 받고 소스코드 폴더에 함께 복사한 후 실행시키기 바랍니다.

tank.png
0.14MB
tankmove.MP3
0.29MB

from tkinter import *
import pygame

class ObjectControlByKeyEvent:
    def __init__(self):
        window = Tk()
        window.title("키보드를 이용한 이미지 컨트롤")
        window.geometry("640x480")
        self.keys = set()
        self.canvas=Canvas(window, bg ="white")
        self.canvas.pack(expand=True, fill=BOTH)
        window.bind("<KeyPress>",self.keyPressHandler)
        window.bind("<KeyRelease>",self.keyReleaseHandler)
        
        self.tankimg = PhotoImage(file="tank.png").subsample(8)
        self.tank = self.canvas.create_image(320,240, image = self.tankimg,tags="tank")

        #Effect sound
        self.sounds = pygame.mixer
        self.sounds.init()
        self.s_effect1 = self.sounds.Sound("tankmove.mp3")
        
        self.canvas.create_text(320,350,font="Times 15 italic bold",text="← 또는 → 키를 입력하시오")
        self.canvas.create_text(320,450,font="Times 15 italic bold",text="Object Control By KeyEvent")

        while True:
            #
            self.display()
            window.after(33)
            window.update()

    def display(self):
        for key in self.keys:
            if key == 39: # right direction key                
                #self.s_effect1.play()
                self.canvas.move(self.tank, 5, 0)
            if key == 37: # left direction key                
                #self.s_effect1.play()
                self.canvas.move(self.tank, -5, 0)

    def keyReleaseHandler(self, event):
        if event.keycode in self.keys:
            #self.s_effect1.stop()
            self.keys.remove(event.keycode)

    def keyPressHandler(self,event):        
        self.keys.add(event.keycode)
        
ObjectControlByKeyEvent()
728x90
반응형
LIST

+ Recent posts