简体   繁体   English

如何从Tkinter按钮获得命令功能?

[英]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.

相关问题 python,tkinter:如何通过所有笔记本选项卡中的单个按钮获取所有变量? - python, tkinter: how all variables can be obtained by single button from all notebook tabs? 如何从第二个 window 在 tkinter 中将 function 作为按钮命令运行 - How to run a function as a button command in tkinter from a 2nd window 通过使用 tkinter 按钮命令,如何将 var 从 function 传递到主 function 中的本地 var - By using tkinter button command, how to pass var from function to local var in main function 如何从Tkinter Scale传递命令以运行功能 - How to pass command to function from Tkinter Scale 从函数而不是按钮命令调用Tkinter框架控制器 - Calling Tkinter frame controller from function rather then button command Tkinter-在类中使用Button命令从另一个类中调用函数 - Tkinter - Using a Button command in a class to call a function from another class 从 tkinter.Button 命令中调用的 function 返回变量 - Returning into variable from function called in tkinter.Button command 如何使用tkinter在新窗口中显示按钮命令的输出? - How can I display output from a button command in a new window using tkinter? 如何为tkinter Button的命令功能结果设置变量? - How to set a variable to the result of command function of a tkinter Button? 如何使用键绑定和按钮命令运行 tkinter function? - How to run tkinter function with both a key bind and a button command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM