gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:56:34 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com