python - tkinter 左键单击 GUI 中的 TAB。如何检测用户何时执行此操作

标签 python function tkinter tabs execute

我有一个用 tkinter 编写的 GUI,一切正常。我想增强它,以便当用户用鼠标左键单击某个选项卡时,将执行一个方法。我认为这将是直截了当的,但我无法让它发挥作用。我的代码是

def f_x():
    print('entered this method')

tab4e = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4e.bind("<Button-1>",f_x())

最佳答案

When the tab changes, it emits the event "<<NotebookTabChanged>>" ,您可以绑定(bind)到:

def handle_tab_changed(event):
    selection = event.widget.select()
    tab = event.widget.tab(selection, "text")
    print("text:", tab)

notebook = ttk.Notebook(...)
...
notebook.bind("<<NotebookTabChanged>>", handle_tab_changed)

使用此事件而不是绑定(bind)到鼠标单击的好处是无论什么原因导致选项卡发生更改,绑定(bind)都会触发。例如,如果您定义用于切换选项卡的快捷键,那么如果用户使用这些快捷键之一,则绑定(bind)到鼠标不会导致您的处理程序触发。

关于python - tkinter 左键单击 GUI 中的 TAB。如何检测用户何时执行此操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44091633/

相关文章:

python - 如何限制widget生成的事件数量

python - 创建一个 Tkinter 按钮到 'clear' 输出

javascript - 如果选中一个复选框,则结束函数

Javascript - 关于对象

tkinter - AttributeError: 'Button' 对象没有属性 'set' - tkinter

python - 在 Python 中将频率转换为音符

python - 导入 __module__ python : why the underscores?

python - 在 Numpy 中向量化特征值计算

python - 如何在Pygame中添加蛇体

JavaScript 定时重定向