gpt4 book ai didi

python - 在 PyQt 中创建动态按钮

转载 作者:太空狗 更新时间:2023-10-29 21:13:23 25 4
gpt4 key购买 nike

我尝试在 PyQt 类中添加一个函数,但它总是返回一个错误。

# Error: TypeError: connect() slot argument should be a callable or a signal, not 'NoneType' # 
def commander (self, arg):
exec arg

def aButton (self, layout, **kwargs):
name = kwargs.pop("name","Button")
command = kwargs.pop("command", "" )
button = QtGui.QPushButton(name)
button.clicked.connect(self.commander(command))
layout.addWidget(button)
return button

也许这里有人可以帮我解决这个问题:')谢谢!

最佳答案

你需要一个函数:

button.clicked.connect(lambda: self.commander(command))

请注意 lambda 将避免函数调用的评估,因此它只会在单击时调用 self.commander(command)

关于python - 在 PyQt 中创建动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10730131/

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