[英]How can the command function be obtained from a Tkinter button?
After a ttk.Button has been created, the name of the callback function can be obtained in various ways. 创建了ttk.Button之后,可以通过多种方式获取回调函数的名称 。 But is there a way to obtain the callback function itself?
但是有没有办法获取回调函数本身呢?
The use case is that I want to use the button's callback function in the callback function to be assigned to another widget. 用例是我想在回调函数中使用按钮的回调函数来分配给另一个小部件。
Currently I'm assigning the button's callback function to an additional attribute of the Button object after creating it. 当前,我在创建按钮后将其回调函数分配给Button对象的其他属性。 That's redundant, though, and I'd like to get the callback function directly from the Button object itself, if possible.
不过,这是多余的,如果可能的话,我想直接从Button对象本身获取回调函数。
No, there is no supported way to get the command via introspection. 不,没有通过自省的方式来获取命令的支持方法。 Your solution of adding it as an attribute is a perfectly reasonable thing to do.
将其添加为属性的解决方案是完全合理的事情。 If you're bothered by the fact it takes two lines of code instead of one, you can subclass the button to hide that detail.
如果您对两行代码而不是两行代码感到困扰,则可以将按钮子类化以隐藏该细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.