在 Tkinter 中绑定鼠标双击
假设针对某个特定应用程序,我们要绑定鼠标双击,以便其执行某个事件或操作。我们可以使用 bind(‘<Double-Button-1>’, handler) 或 bind(‘<Double-Button-2>’, handler) 方法将鼠标左右按钮与处理程序或回调函数绑定在一起。
示例
在本示例中,我们将创建一个包含按钮的应用程序。当我们双击按钮时,它将打开一个弹出窗口。
#Import required libraries
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the window
win.geometry("750x250")
#Define a function
def handler(e):
top= Toplevel(win)
top.geometry("600x200")
Label(top, text= "Hey There!", font= ('Helvetica 15 bold')).pack(pady=30)
#Define a Label in Main window
Label(win, text= "Double Click to Open the Popup",font=('Helvetica 15 underline')).pack(pady=30)
#Create a Button
ttk.Button(win, text= "Click", command=lambda:handler).pack(pady=20)
#Bind the Double Click with the Handler
win.bind('<Double-Button-1>', handler)
win.mainloop()输出
现在,运行以上代码以显示窗口。
双击按钮“点击”以打开一个弹出窗口。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP