在 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()

输出

现在,运行以上代码以显示窗口。

双击按钮“点击”以打开一个弹出窗口。

更新于:2021 年 5 月 3 日

3K+ 浏览量

开启你的 职业

完成此课程以取得认证

开始吧
广告
© . All rights reserved.