在 PyQt 中创建动态按钮 [英] Create dynamic button in PyQt

查看:42
本文介绍了在 PyQt 中创建动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to add a function in a PyQt class, but it always returns me an error.

# 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

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

May be someone here can help me to solve that :') Thx !

推荐答案

你需要一个函数:

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

注意 lambda 会避免对函数调用的评估,所以它只会在点击时调用 self.commander(command)

Note the lambda will avoid the evaluation of the function call, so it'll call self.commander(command) only when clicked

这篇关于在 PyQt 中创建动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆