当前位置:网站首页>Gui Gui programming (XIII) - event handling
Gui Gui programming (XIII) - event handling
2022-07-06 20:18:00 【Progress Xiaobai】
Thank you for opening Xiaobai's article
“ I hope you have made a little progress today , One step closer to a better life !”
Catalog
event Object common properties
Test keyboard and mouse events
Summary of various event binding methods
Summary of various event binding methods
Preface
One individual GUI The whole application life cycle is in a message cycle (event loop) in . It waits for the event to happen , And deal with it accordingly .Tkinter Provides a mechanism for handling related events . Handling functions can be bound to various events of various controls .
widget.bind(event, handler)
If relevant events occur raw , handler The function will be triggered , Event object event Will pass to handler function .
Mouse and keyboard events
Code | explain |
<Button-1> <ButtonPress-1> <1> | Press the left mouse button . 2 Right click ,3 Represents the middle key ; |
<ButtonRelease-1> | Release the left mouse button |
<B1-Motion> | Press and hold the left mouse button to move |
<Double-Button-1> | Double left click |
<Enter> | The mouse pointer enters a component area |
<Leave> | The mouse pointer leaves a component area |
<MouseWheel> | Scroll wheel ; |
<KeyPress-a> | Press down a key ,a You can use another key instead of |
<KeyRelease-a> | Release a key . |
<KeyPress-A> | Press down A key ( uppercase A) |
<Alt-KeyPress-a> | Press at the same time alt and a;alt You can use ctrl and shift replace |
<Double-KeyPress-a> | Two quick clicks a |
<Control-V> | CTRL and V The keys are pressed simultaneously ,V It can be changed to other key positions |
event Object common properties
name | explain |
char | Key characters , Valid only for keyboard events |
keycode | Key code , Valid only for keyboard events |
keysym | Key name , Only valid for keyboard events, such as pressing the spacebar : Keyed char: Keyed keycode:32 Keyed keysym:space For example, press a key : Keyed char:a Keyed keycode:65 Keyed keysym:a |
num | Mouse button , Valid only for mouse events |
type | The type of event triggered |
widget | The component that caused the event |
width,height | The size of the changed component , only Configure It works |
x,y | Current mouse position , Relative to the parent container |
x_root,y_root | Current mouse position , Relative to the whole screen |
Test keyboard and mouse events
# coding=utf-8
# Test keyboard and mouse events
from tkinter import *
root = Tk();root.geometry("530x300")
c1 = Canvas(root,width=300,height=300,bg="pink")
c1.pack()
def mouseTest(event):
print(" Left click position ( Relative to the parent container ):{0},{1}".format(event.x,event.y))
print(" Left click position ( Relative to the screen ):{0},{1}".format(event.x_root,event.y_root))
print(" Event bound components :{0}".format(event.widget))
def testDrag(event):
c1.create_oval(event.x,event.y,event.x+1,event.y+1)
def keyboardTest(event):
print(" Keyed keycode:{0}, Keyed char:{1}, Keyed keysym:{2}"
.format(event.keycode,event.char,event.keysym))
def press_a_test(event):
print("press a")
def release_a_test(event):
print("release a")
c1.bind("<Button-1>",mouseTest)
c1.bind("<B1-Motion>",testDrag)
root.bind("<KeyPress>",keyboardTest)
root.bind("<KeyPress-a>",press_a_test) # Only for lowercase a, uppercase A No use
root.bind("<KeyRelease-a>",release_a_test)
root.mainloop()
give the result as follows :
Casually write numbers , events , Mouse events and keyboard events .
lambda Expression details
lambda The expression defines an anonymous function , Only suitable for simple input parameters , Simple calculation returns results , Not suitable for complex functions .lambda The defined anonymous function also has input 、 There's also output , Just no name . The syntax is as follows :
lambda Parameter value list : expression The parameter value list is the input . The structure of expression evaluation is output .
Write One individual most Jane single Of case example : add3args = lambda x,y,z:x+y+z #print(add3args(10,20,30))
above lambda The expression is equivalent to the following function definition :
def add3args(x,y,z):
return x+y+z
lambda The parameter value list of the expression can be as follows :
lambda Format | explain |
lambda x, y: x*y | The function input is x and y, The output is their product x*y |
lambda:None | Function has no input parameters , The output is None |
lambda:aaa(3,4) | Function has no input parameters , The output is aaa(3,4) The knot of fruit |
lambda *args: sum(args) | The input is any number of parameters , The output is their sum |
lambda **kwargs: 1 | The input is any key value pair parameter , The output is |
️ test lambda usage
from tkinter import *
root = Tk();root.geometry("270x50")
def mouseTest1():
print("command The way , Simple case : It does not involve obtaining event object , have access to ")
def mouseTest2(a,b):
print("a={0},b={1}".format(a,b))
Button(root, text=" test command1",
command=mouseTest1).pack(side="left")
Button(root, text=" test command2",
command=lambda: mouseTest2(" Small ", " white ")).pack(side="left")
root.mainloop()
Select the event in the pop-up box .
Summary of various event binding methods
Binding of component objects
adopt command Attribute binding ( Suitable for simple without obtaining event object )
Button(root,text=” Sign in ”,command=login)
adopt bind() Method binding ( Suitable for the need to obtain event object )
c1 = Canvas(); c1.bind(“<Button-1>”,drawLine)
Binding of component classes
Call the object's bind_class function , Bind all components of this component class to events :
w.bind_class(“Widget”,”event”,eventhanler)
Summary of various event binding methods
from tkinter import *
root = Tk();root.geometry("270x30")
def mouseTest1(event):
print("bind() Methods the binding , Can get event object ")
print(event.widget)
def mouseTest2(a, b):
print("a={0},b={1}".format(a, b))
print("command Methods the binding , You can't get it directly event object ")
def mouseTest3(event):
print(" Right click on the event , Bound to all buttons !!")
print(event.widget)
b1 = Button(root, text=" test bind() binding ")
b1.pack(side="left")
# bind Bind events by
b1.bind("<Button-1>", mouseTest1)
# command Property is directly bound to the event
b2 = Button(root, text=" test command2",
command=lambda: mouseTest2(" The small white ", " come on. "))
b2.pack(side="left")
# To all Button Buttons are bound to right-click Events <Button-2>
b1.bind_class("Button", "<Button-2>", mouseTest3)
root.mainloop()
Looking back
GUI Graphical user interface programming ( One ): (278 Bar message ) GUI Graphical user interface programming ( One )_am_student The blog of -CSDN Blog
GUI Graphical user interface programming ( Two ):
GUI Graphical user interface programming ( 3、 ... and ):
GUI Graphical user interface programming ( Four ):
GUI Graphical user interface programming ( 5、 ... and ):
边栏推荐
猜你喜欢
B-jiege's tree (pressed tree DP)
OceanBase社区版之OBD方式部署方式单机安装
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
[network planning] Chapter 3 data link layer (3) channel division medium access control
Ideas and methods of system and application monitoring
Leetcode question 283 Move zero
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
Introduction of Xia Zhigang
Redisson bug analysis
01 basic introduction - concept nouns
随机推荐
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
系统与应用监控的思路和方法
青龙面板白屏一键修复
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
Standardized QCI characteristics
js获取浏览器系统语言
Unity making plug-ins
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
Appx代码签名指南
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
语音识别(ASR)论文优选:全球最大的中英混合开源数据TALCS: An Open-Source Mandarin-English Code-Switching Corpus and a Speech
22-07-05 七牛云存储图片、用户头像上传
[cloud native and 5g] micro services support 5g core network
Web security - payload
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
01 基础入门-概念名词
SSH connection denied
Node.js: express + MySQL实现注册登录,身份认证
Tencent Android development interview, basic knowledge of Android Development
Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech