我有一个用 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/