简体   繁体   中英

How to handle a widget command/function call from another class in Python?

When the button is pressed, I want to handle that function call not in the class that the button is in but rather in another class. So here's the following code on what I'm trying to achieve:

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!"

Please let me know how this is achieved and thank you so much!

Note: I edited my response because i dind't understand your question properly.

In python you can pass function as parameter :

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)

Static Functions:

If the class is already defined and the function you want to pass is static, you should be able to do something like this:

class TestClass:
    def testButtonPressed(self):
        print "Button is pressed!"    

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

Remember: when passing a function as an argument, you need to remove the parenthesis '()'. If you do not, you'd be passing what the function returns, and not the function itself.

.

Non-Static Functions:

If the function you want to pass is not static (needs to be called within an instance of the class), you'd have to have a reference to that instance:

class TestClass:
    def __init__(self):
        self.message = "Button is pressed!"

    def testButtonPressed(self):
        print self.message    

class TestButton:
    def __init__(self, root):
        instance = TestClass()

        self.testButton = Button(root, text="Test Button", command=instance.testButtonPressed).grid(row=11, column=0)

or, if the instance is not within the scope of the class:

instance = TestClass()

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

test = TestButton(root, instance)

Note: Non-Static methods can be usually identified by having a 'self' parameter: Eg: def function(self)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM