Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách gán sự kiện chuột trái giữ nhấn trong Tkinter

Bind <B1-Motion> (di chuyển chuột khi giữ trái) và <ButtonRelease-1> (thả trái) với hàm xử lý.

Ví dụ

from tkinter import *

win = Tk()
win.geometry("750x250")

def handler1(e):
    print("You are moving the Mouse with the Left Button Pressed.")

def handler2(e):
    print("Button Released")

Label(win, text="Move the Mouse with the Left Button Pressed", font='Helvetica 15 underline').pack(pady=30)

win.bind('', handler1)
win.bind('', handler2)

win.mainloop()

Kết quả

Kéo chuột trái → in liên tục "You are moving...". Thả chuột → in "Button Released".

Cách gán sự kiện chuột trái giữ nhấn trong Tkinter