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

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

问题描述

我试图按需调用默认的窗口小部件行为.基于此答案,我对绑定标签和标签列表有所了解.

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

例如,假设我有两个按钮,并且每当我左键单击右按钮时,我都希望在其标签列表的第一个按钮的标签中具有特定项目的绑定标签行为:

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的所有(理想情况下是我所选择的)绑定标签回调:

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?

推荐答案

要调用绑定到给定序列(例如<Button-1>)的回调,您可以执行widget.event_generate(<sequence>, **kwargs).例如,对于<Button-1>事件,可选的关键字参数可以为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>')

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

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