如何使用默认的 bindtag 行为作为方法? [英] How to use default bindtag behavior as methods?

查看:31
本文介绍了如何使用默认的 bindtag 行为作为方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按需调用默认小部件行为.基于这个答案,我对绑定标签和标签列表有所了解.

I am trying to invoke default widget behavior on demand. Based on this answer I know a bit about bindtags, and taglists.

假设例如我有两个按钮,我想在它的标签列表中拥有特定项目的 bindtag 行为,即第一个按钮,每当我左键单击右侧按钮时:

Let's say for example I have two buttons, and I want to have the bindtag behavior of a specific item in its taglist, of first button, whenever I left click on the right button:

import tkinter as tk

root = tk.Tk()

first_btn = tk.Button(root, text="1st")
second_btn = tk.Button(root, text="2nd")

second_btn.bind("<Button-1>", "expect the bindtag operation here")

first_btn.pack()
second_btn.pack()

root.mainloop()

我还没有为 first_btn 指定 command=... 因为我需要有按钮推送的效果,例如我的 second_btn绑定.

I haven't specified command=... for first_btn as I need to have the very effect of button push for example with my second_btn binding.

或者作为另一个例子,我想让 second_btn 调用 first_btn 的所有(最好是我选择的)bindtag 回调:

or as another example, I want to have second_btn to invoke all(ideally of my choosing) bindtag callbacks of first_btn:

import tkinter as tk

root = tk.Tk()

first_btn = tk.Button(root, text="1st")
second_btn = tk.Button(root, text="2nd", command="first_btn bindtag callbacks")

first_btn.pack()
second_btn.pack()

root.mainloop()

<小时>

或者,绑定到绑定标签的操作的引用名称(如果有的话)是什么?


Or, what are reference names(if any) to the operations attached to bindtags?

推荐答案

要调用绑定到给定序列的回调(例如 ),您可以执行 widget.event_generate(, **kwargs).例如,对于 事件,可选的关键字参数可以是 x=0y=0.这将触发与序列关联的所有回调(它们是否与 bindbind_classbind_all 绑定无关紧要).

To invoke the callback bound to a given sequence (e.g. <Button-1>), you can do widget.event_generate(<sequence>, **kwargs). The optional keyword arguments can be x=0, y=0 for a <Button-1> event for instance. This will trigger all callbacks associated with the sequence (it does not matter whether they were bound with bind, bind_class or bind_all).

在下面的例子中,当第二个按钮被点击时,它会降低第一个按钮,就像它也被点击一样:

In the below example, when the second button is clicked, it lowers the first one as if it were clicked too:

import tkinter as tk

root = tk.Tk()

first_btn = tk.Button(root, text="1st")
second_btn = tk.Button(root, text="2nd", command=lambda: first_btn.event_generate('<Button-1>'))

first_btn.pack()
second_btn.pack()

root.mainloop()

但是,从命令行进行交互测试时,生成键盘事件通常不起作用,因为小部件没有键盘焦点.因此,使用以下代码,当点击按钮时,View 菜单将打开,就像我们完成了Alt+v:

However, when doing tests interactively from the command line, generating keyboard events often does not work because the widget does not have keyboard focus. So, with the following code, when clicking on the button, the View menu will open as if we had done Alt+v:

import tkinter as tk

root = tk.Tk()
menubar = tk.Menu(root)
viewMenu = tk.Menu(menubar, tearoff=0)
viewMenu.add_command(label="Item 1")
viewMenu.add_command(label="Item 2")
menubar.add_cascade(menu=viewMenu, label="View", underline=0)
root.config(menu=menubar)
tk.Button(root, text='Test', command=lambda: root.event_generate('<Alt-v>')).pack()
root.mainloop()

但是当我从 IPython QtConsole 执行 root.event_generate('<Alt-v>') 时,没有任何反应.解决方法是在生成事件之前强制键盘焦点:

But when I do root.event_generate('<Alt-v>') from the IPython QtConsole, nothing happens. The workaround is to force keyboard focus before generating the event:

root.focus_force()
root.event_generate('<Alt-v>')

这篇关于如何使用默认的 bindtag 行为作为方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆