zoukankan      html  css  js  c++  java
  • tkinter学习-- 八、事件event

    事件处理

    一个GUI应用整个生命周期都处在一个消息循环(eventloop)中
    它等待事件的发生,并作出相应的处理
    Tkinter提供了用以处理相关事件的机制.处理函数可被绑定给各个控件的各种事件
    widget.bind(event,handler)

    如果相关事件发生,handler函数会被触发,事件对象event会传递给handler函数

     

    from tkinter import *
    root = Tk();root.geometry("530x300")
    c1 = Canvas(root,width=200,height=200,bg="green")
    c1.pack()
    def   mouseTest(event):
        print("鼠标左键单击位置(相对于父容器):{0},{1}".format(event.x,event.y))
        print("鼠标左键单击位置(相对于屏幕):{0},{1}".format(event.x_root,event.y_root))
        print("事件绑定的组件:{0}".format(event.widget))
    def testDrag(event):
        c1.create_oval(event.x,event.y,event.x+1,event.y+1)
    def  keyboardTest(event):
        print("键的 keycode:{0},键的 char:{1},键的 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) #只针对小写的a,大写的 A 不管用
    root.bind("<KeyRelease-a>",release_a_test)
    root.mainloop()
    

      

  • 相关阅读:
    创建工作窗口
    windows下关闭进程树
    VC socket api使用引入
    实现PC延迟执行函数
    CxImage实现9PNG
    bzoj 3211 花神游历各国
    codevs 3287 货车运输 NOIP2013提高组
    bzoj 3732 Network
    codevs 2370 小机房的树
    图论-最近公共祖先-在线树上倍增
  • 原文地址:https://www.cnblogs.com/yescarf/p/13901873.html
Copyright © 2011-2022 走看看