(Python) event object in tkinter

1 Introduction

I have read many blogs. Regarding the event object, everyone is basically using it directly in the main program, or in the function, almost no in the class (class). There are some precautions for using event in class . Here I will introduce the program as an example. Let me talk about the use of event objects first.

2. Mouse and keyboard events

Here is a list of commonly used events and their descriptions, and how to use them is in the program.
Insert picture description here

3. Common attributes of event object

The calling method of the property is: event. property name
Insert picture description here

4. Sample program

4.1 Define class

class Application(Frame):

    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createwidget()

4.2 createwidget() function

The createwidget() function creates the canvas and binds a series of events

    def createwidget(self):
        # global c1
        self.c1 = Canvas(self, width=200, height=200, bg="green") #创建画布
        self.c1.pack()
        self.c1.bind("<Button-1>", self.mouseTest)   # 当鼠标左键点击时,触发事件
        self.c1.bind("<B1-Motion>", self.testDrag)   # 当按住鼠标左键并拖动时, 触发事件
        self.master.bind("<KeyPress>", self.keyboardTest)   # 当按下键盘时就触发
        self.master.bind("<KeyPress-a>", self.press_a_test)  # 按下a键时,触发事件
        self.master.bind("<KeyRelease-a>", self.release_a_test)  # 释放a键时,触发事件

4.3 bound event function


    def mouseTest(self, event):
    	# 显示鼠标点击点相对于c1的位置
        print("鼠标左键单击位置(相对于父容器)为:{}{}".format(event.x, event.y))  
        # 显示鼠标点击点相对于屏幕的位置
        print("鼠标左键单击位置(相对于屏幕)为:{}{}".format(event.x_root, event.y_root)) 
       # 显示事件触发的控件
        print("事件绑定的组件:{}".format(event.widget))  

    def testDrag(self, event):
        # 在鼠标当前位置与当前位置+2的位置创建一个椭圆,拖动较慢时实现的是画图功能
        self.c1.create_oval(event.x, event.y, event.x+2, event.y+2,fill="black")

    def keyboardTest(self, event):
        print("键的keycode:{},键的char:{},键的keysym:{}"
              .format(event.keycode, event.char, event.keysym))

    def press_a_test(self, event):
        print("press a")

    def release_a_test(self, event):
        print("release a")

4.4 Function test

First, after the program runs, the interface is displayed as follows:
Insert picture description here
Click the canvas area, the program executes the mouseTest function, and the result is displayed as follows:

Insert picture description here
Drag the mouse, the program executes the testDrag function, drag the mouse to draw a picture, the display is as follows:
Insert picture description here
press any key, the program executes the keyboardTest function, and the result of pressing the key 4 times is
displayed as follows: the result of
Insert picture description here
pressing and releasing the a key is as follows:
Insert picture description here

4.5 All codes

from tkinter import *

class Application(Frame):

    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createwidget()

    def createwidget(self):
        # global c1
        self.c1 = Canvas(self, width=200, height=200, bg="green") #创建画布
        self.c1.pack()
        self.c1.bind("<Button-1>", self.mouseTest)   # 当鼠标左键点击时,触发事件
        self.c1.bind("<B1-Motion>", self.testDrag)   # 当按住鼠标左键并拖动时, 触发事件
        self.master.bind("<KeyPress>", self.keyboardTest)   # 当按下键盘时就触发
        self.master.bind("<KeyPress-a>", self.press_a_test)  # 按下a键时,触发事件
        self.master.bind("<KeyRelease-a>", self.release_a_test)  # 释放a键时,触发事件

    def mouseTest(self, event):
   		 # 显示鼠标点击点相对于c1的位置
        print("鼠标左键单击位置(相对于父容器)为:{}{}".format(event.x, event.y))  
        # 显示鼠标点击点相对于屏幕的位置
        print("鼠标左键单击位置(相对于屏幕)为:{}{}".format(event.x_root, event.y_root)) 
        # 显示事件触发的控件
        print("事件绑定的组件:{}".format(event.widget))  

    def testDrag(self, event):
        # 在鼠标当前位置与当前位置+2的位置创建一个椭圆,拖动较慢时实现的是画图功能
        self.c1.create_oval(event.x, event.y, event.x+2, event.y+2,fill="black")

    def keyboardTest(self, event):
        print("键的keycode:{},键的char:{},键的keysym:{}"
              .format(event.keycode, event.char, event.keysym))

    def press_a_test(self, event):
        print("press a")

    def release_a_test(self, event):
        print("release a")


if __name__ == '__main__':

    root = Tk()
    root.title("event测试")
    root.geometry("300x300")
    app = Application(root)
    root.mainloop()

Guess you like

Origin blog.csdn.net/weixin_45727931/article/details/107949930