파이썬(Python)/tkinter

[Python]tkinter 마우스 클릭 이벤트 시 그림 이동하기(Mouse, event, click, move)

끄적끄적아무거나 2022. 2. 15. 08:35
반응형

 

목차

     

     

     

     

     

    tkinter 마우스 클릭 이벤트 Syntax

     

    파이썬의 tkinter를 사용해서 키보드로 이벤트 발생 시 도형을 만들고 이동하는 예제를 구현해 보았습니다(https://scribblinganything.tistory.com/280). 이번에는 마우스 왼쪽, 오른쪽, 가운데 휠을 눌러 도형을 이동하는 예제를 구현해 보겠습니다. 

     

    event 를 사용하기 위해서는 tkinter에서 만든 객체를 bind를 사용해서 함수와 묶어야 합니다. 묶는 방법은 아래와 같습니다. 

     

    app.bind("<Button-1>", 함수명)

     

    Button-1 : 마우스 왼쪽 버튼 클릭

    Button-2 : 마우스 가운데 휠 클릭

    Button-3 : 마우스 오른쪽 버튼 클릭

     

    함수 명 : 이벤트 발생 시 실행하고자 하는 함수 명

     

     

    아래 실제 예제를 통해 이해해 보도록 하겠습니다.

     

     

     

    tkinter로 도형 만들고 마우스 클릭으로 도형 이동하기 예제

     

    예제 코드>>

    from tkinter import * 
    
    app = Tk() 
    width = 600 
    height = 400 
    pos_x = width/2 
    pos_y = height/2 
    canvas = Canvas(app, width=width, height=height, bg="white") 
    canvas.pack(padx=10, pady=10) 
    
    def making_shape(event): 
        global shapes
        if event.char == "o": 
            canvas.delete("all") 
            shapes = canvas.create_oval(width/2, height/2, pos_x+30, pos_y+30, fill="black") 
        elif event.char == "l": 
            canvas.delete("all") 
            shapes = canvas.create_line(width/2, height/2, pos_x+30, pos_y+30, fill="black") 
        elif event.char == "s": 
            canvas.delete("all") 
            shapes = canvas.create_polygon(width/2, height/2, pos_x+15, height/2, pos_x+15, pos_y+15, width/2, pos_y+15, fill="black")
    
    def move_right(event): 
        pos_x = 10 
        pos_y = 0 
        canvas.move(shapes, pos_x, pos_y) 
    
    def move_left(event): 
        pos_x = -10 
        pos_y = 0 
        canvas.move(shapes, pos_x, pos_y) 
        
    
    app.bind("<Button-3>", move_right) 
    app.bind("<Button-1>", move_left) 
    app.bind("<Key>", making_shape) 
    
    app.title('scribblinganything.tistory.com') 
    app.geometry("700x450") 
    
    app.mainloop()

    11~21번 라인: 도형을 만드는 함수, 키보드에서 o를 클릭하면 동그라미, 타원을 만들고, l을 클릭하면 라인을 그리고 s를 클릭하면 사각형을 만듭니다. 

    23~31번 라인: 만들어진 도형을 마우스 오른쪽, 왼쪽 버튼을 사용해서 움직입니다.

    34~36번 라인: 앞서 만든 함수를 마우스와 키보드와 연결해 줍니다.

     

     

    결과>>

    위 결과는 키보드 s를 클릭하고 마우스 버튼을 오른쪽 왼쪽을 눌러 이동 했습니다.

     

     

     

     

    tkinter로 만들어진 도형을 마우스 클릭한 위치로 이동하기 예제

     

    이번 예제는 파이썬 tkinter로 이미 만들어진 도형을 이용해서 마우스 클릭으로 클릭한 위치로 움직이는 예제입니다.

     

    예제 코드>>

    from tkinter import * 
    
    app = Tk() 
    width = 600 
    height = 400 
    canvas = Canvas(app, width=width, height=height, bg="white") 
    canvas.pack(padx=10, pady=10) 
    
    def callback_mouse(event): 
        print(event.x,event.y)
        canvas.delete("all") 
        shapes = canvas.create_polygon(event.x, event.y, event.x+15, event.y, event.x+15, event.y+15, event.x, event.y+15, fill="black")
    
    app.bind("<Button-1>", callback_mouse) 
    
    app.title('scribblinganything.tistory.com') 
    app.geometry("700x450") 
    
    app.mainloop()

    9~12번 라인: 사각형 도형을 만들고 마우스 클릭 이벤트 발생 시 해당 위치를 print 해준 후 그 위치에 도형을 다시 만들어 주는 함수 입니다.

    14번 라인: 마우스 왼쪽 버튼과 callback_mouse 함수를 연결해 줍니다.

     

     

     

    결과>>

    182 248
    336 172
    133 149
    412 296
    470 205

     

     

    관련글>>https://scribblinganything.tistory.com/280

     

    (파이썬) 키보드 입력으로 도형 생성, 움직이는 그림 예제 (canvas, tkinter)

    Python - 키보드 event 값 받아서 도형 만들고 키보드 방향키로 도형 움직여 보기 이번 예제는 제목과 같이 tkinter의 canvas 기능을 사용해서 도형을 만들고, 방향키 입력에 맞춰 해당 도형이 움직이는

    scribblinganything.tistory.com

     

     

    반응형