python - tkinter 中有点击事件处理程序吗?

标签 python tkinter events click mouse

伙计们,我正在 tkinter 中编写 GUI。我想处理点击事件,就像用户在 GUI 上使用“左”鼠标按钮时一样。这是为了在用户点击时播放声音。那么有没有类似的功能:

onClick(lambda: play()) #call a function

提前致谢:)

最佳答案

您可以向 Canvas 添加点击事件。

这样的事情应该有效:

root = Tk()

def on_click(event):
    print("you clicked")

canvas = Canvas(root, width=800, height=600)
canvas.bind("<Button-1>", on_click)
canvas.pack()

# Canvas.focus_set is required if the window already contains a widget that has keyboard/input focus
canvas.focus_set()

root.mainloop()

以下是使用此方法的一些示例:https://python-course.eu/tkinter/events-and-binds-in-tkinter.php

关于python - tkinter 中有点击事件处理程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72211339/

相关文章:

vue.js - 使用 v-on 对象语法传递事件修饰符

Javascript removeEventListener 不工作 - 事件监听器仍然存在

python - python中CountVectorizer稀疏矩阵的列名

Python Inspect - 查找 GAE db.model 类中属性的数据类型

python - 在 Python ie 的自定义发行版上安装 Ehem 时遇到问题。 Maya(3D/CGI 软件)

python - 使用 TKinter 编辑窗口

python - 无法在 pandas NamedAgg 函数中使用 lambda 函数

python - Tkinter 中的视频输出?

python - 在 tkinter GUI 中更新 matplotlib 图

WPF区分编码-SelectionChanged和鼠标-SelectionChanged