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

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/

10-09 03:17