python - 如何处理来自 Python 中另一个类的小部件命令/函数调用?

标签 python python-3.x python-2.7

按下按钮时,我想不在按钮所在的类中处理该函数调用,而是在另一个类中处理该函数调用。下面是我想要实现的目标的代码:

class TestButton:
    def __init__(self, root):
        self.testButton = Button(root, text ="Test Button", command = testButtonPressed).grid(row = 11, column = 0)
        #testButtonPressed is called in the TestButton class.

class TestClass:

    #The testButtonPressed function is handled in the TestClass. 
    def testButtonPressed():
        print "Button is pressed!"

请告诉我这是如何实现的,非常感谢!

最佳答案

注意:我编辑了我的回复,因为我没有正确理解您的问题。

在 python 中,您可以将函数作为参数传递:

class TestButton:
    def __init__(self, root, command):
        self.testButton = Button(root, text ="Test Button", command = command).grid(row = 11, column = 0)
        #testButtonPressed is called in the TestButton class.


class TestClass:

    #The testButtonPressed function is handled in the TestClass. 
    def testButtonPressed(self):
        print "Button is pressed!"

TestButton(root, TestClass().testButtonPressed)

关于python - 如何处理来自 Python 中另一个类的小部件命令/函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55942945/

相关文章:

python - 警告 :tensorflow:Model was constructed with shape (None, ....)

python - 如何在 python/pandas 的循环中迭代使用变量名

python - 我可以在 python 中使用库抽象吗?

mysql - 插入多行时 commit() 的最佳实践

python - in和列表的索引函数[Python]

python - python中的错误文本编码

python - 如何检查用户输入对比萨列表/元组是否有效?

python - Urllib2- 抓取并显示任意语言页面,编码问题

json - 访问 json 树的叶子

python - 为什么要创建不同的 HOST 和 PORT 变量,然后使用它们来创建 ADDR 变量?