zoukankan      html  css  js  c++  java
  • Python:GUI之tkinter学习笔记3事件绑定(转载自https://www.cnblogs.com/progor/p/8505599.html)

    Python:GUI之tkinter学习笔记3事件绑定

     

    相关内容:

    • command
    • bind
    • protocol

    首发时间:2018-03-04 19:26


    command:

    • command是控件中的一个参数,如果使得command=函数,那么点击控件的时候将会触发函数
    • 能够定义command的常见控件有: Button、Menu…
    • 调用函数时,默认是没有参数传入的,如果要强制传入参数,可以考虑使用lambda
    复制代码
    from tkinter import *
    root=Tk()
    
    def prt():
        print("hello")
    def func1(*args,**kwargs):
        print(*args,**kwargs)
    hello_btn=Button(root,text="hello",command=prt)#演示
    hello_btn.pack()
    
    args_btn=Button(root,text="获知是否button事件默认有参数",command=func1)#获知是否有参数,结果是没有
    
    args_btn.pack()
    btn1=Button(root,text="传输参数",command=lambda:func1("running"))#强制传输参数
    
    btn1.pack()
    
    root.mainloop()
    复制代码

    bind:

    • bind的用法:控件.bind(event, handler),其中event是tkinter已经定义好的的事件,handler是处理器,可以是一个处理函数,如果相关事件发生, handler 函数会被触发, 事件对象 event 会传递给 handler 函数
    • 基本所有控件都能bind
    • 常见event有:
      • 鼠标单击事件:鼠标左键点击为 <Button-1>, 鼠标中键点击为 <Button-2>, 鼠标右键点击为 <Button-3>, 向上滚动滑轮为 <Button-4>, 向下滚动滑轮为 <Button-5>.
      • 鼠标双击事件.:鼠标左键点击为 <Double-Button-1>, 鼠标中键点击为 <Double-Button-2>, 鼠标右键点击为 <Double-Button-3>.
      • 鼠标释放事件:鼠标左键点击为 <ButtonRelease-1>, 鼠标中键点击为 <ButtonRelease-2>, 鼠标右键点击为 <ButtonRelease-3>. 鼠标相对当前控件的位置会被存储在 event 对象中的 x 和 y 字段中传递给回调函数.
      • 鼠标移入控件事件:<Enter>
      • 获得焦点事件:<FocusIn>
      • 鼠标移出控件事件: <Leave>
      • 失去焦点事件:<FocusOut>
      • 鼠标按下移动事件:鼠标左键点击为 <B1-Motion>, 鼠标中键点击为 <B2-Motion>, 鼠标右键点击为 <B3-Motion>. 鼠标相对当前控件的位置会被存储在 event 对象中的 x 和 y 字段中传递给回调函数.
      • 键盘按下事件:<Key>,event中的keysym ,keycode,char都可以获取按下的键【其他想要获取值的也可以先看看event中有什么】
      • 键位绑定事件:<Return>回车键,<BackSpace>,<Escape>,<Left>,<Up>,<Right>,<Down>…….
      • 控件大小改变事件:<Configure>,新的控件大小会存储在 event 对象中的 width 和 height 属性传递. 有些平台上该事件也可能代表控件位置改变.
    • Event中的属性:
      • widget:产生事件的控件
      • x, y:当前鼠标的位置
      • x_root, y_root:当前鼠标相对于屏幕左上角的位置,以像素为单位。
      • char:字符代码(仅限键盘事件),作为字符串。
      • keysym:关键符号(仅限键盘事件)。
      • keycode:关键代码(仅限键盘事件)。
      • num:按钮号码(仅限鼠标按钮事件)。
      • width, height:小部件的新大小(以像素为单位)(仅限配置事件)。
      • type:事件类型。
     
     
     
    复制代码
    from tkinter import *
    root=Tk()
    root.geometry("200x200")
    text=Text(root)
    text.pack()
    
    def func(event):
        print(event)
    def func_release(event):
        print("release")
    #单击
    # text.bind("<Button-1>",func)
    # root.bind("<Button-1>",func)
    #双击
    # text.bind("<Double-Button-1>",func)
    # 鼠标释放
    # text.bind("<ButtonRelease-1>",func_release)
    #鼠标移入
    # text.bind("<Enter>",func)
    #鼠标按住移动事件
    # text.bind("<B1-Motion>",func)
    #键盘按下事件
    # text.bind("<Key>",func)
    
    #键位绑定事件
    # def func3(event):
    #     print("你按下了回车!")
    # text.bind("<Return>",func3)
    
    
    #实现的一个拖拽功能
    def func4(event):
        # print(event)
        x=str(event.x_root)
        y=str(event.y_root)
        root.geometry("200x200+"+x+"+"+y)
    
    text.bind("<B1-Motion>",func4)
    
    
    
    
    root.mainloop()
    复制代码

    补充:如果想要传参,可以使用lambda:

    text.bind("<Button-1>",lambda event:func(event,"hello"))
     image

    image


    protocol:

    • protocol的使用:控件.protocol(protocol,handler),其中控件为窗口对象(Tk,Toplevel)
    • 常见protocol有:
      • WM_DELETE_WINDOW:最常用的协议称为WM_DELETE_WINDOW,用于定义用户使用窗口管理器明确关闭窗口时发生的情况。如果使用自己的handler来处理事件的话,这时候窗口将不会自动执行关闭
      • WM_TAKE_FOCUS,WM_SAVE_YOURSELF:[这两个不知道什么来的。]
      • 更多需参考ICCCM文档
    • 注意:要留心协议的写法,在作为字符串填入时不要加多余的空格
     
    复制代码
    from tkinter import *
    import tkinter.messagebox
    root=Tk()
    root.geometry("200x200")
    def func1():
        if tkinter.messagebox.askyesno("关闭窗口","确认关闭窗口吗"):
            root.destroy()
    
    root.protocol("WM_DELETE_WINDOW",func1)
    
    root.mainloop()
  • 相关阅读:
    [导入]Zanzarah: The Hidden Portal Cheats
    [导入]Zanzarah Walkthrough
    Each的实现
    关于SQL命令中不等号(!=,<>)
    获取当前运行函数及调用函数
    MVC3.0学习2Razor视图引擎基础语法
    MVC 3.0 学习1
    java.io.IOException: Too many open files错误解决方案
    linux下执行命令输出乱码解决方案
    通用代码引擎生成工具 XDoclet 介绍
  • 原文地址:https://www.cnblogs.com/shiyongge/p/10223609.html
Copyright © 2011-2022 走看看