在循环中创建PyQt5按钮:所有按钮触发相同的回调 [英] Creating PyQt5 buttons in a loop: all buttons trigger the same callback

查看:255
本文介绍了在循环中创建PyQt5按钮:所有按钮触发相同的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该提一下,我已经阅读了这些内容,但仍然无法实现自己的目标:

I should mention that I've read these but I'm still unable to achieve my goal:

[在for循环中使用字典创建按钮不起作用

[ QtCore.循环中的QObject.connect仅影响最后一个实例

我的目标是制作一个Linux启动器"应用程序.按钮的创建,放置等工作就像一种魅力,但是有一个问题-所有按钮都触发相同的回调-在按钮创建循环中最后一个要连接的回调.

My goal is to make a linux 'launcher' application. Button creation, placement, etc. is working like a charm but there's one problem - all buttons trigger the same callback - the last one to be connected in the button creation loop.

这是脚本的基本版本,用于说明我要执行的操作:

Here's a basic version of the script to illustrate what I'm trying to do:

class App(QMainWindow):

    def launch(self, filepath):
        subprocess.run(filepath)


    def __init__(self):
        super(App, self).__init__()

        for btn in matrix:

            filepath = matrix[btn]['path']
            icon = matrix[btn]['setIcon']
            posx = matrix[btn]['posx']
            posy = matrix[btn]['posy']

            matrix[btn] = QToolButton(self)
            matrix[btn].setIcon(QIcon(icon))
            matrix[btn].setIconSize(QSize(64, 64))
            matrix[btn].resize(100, 100)
            matrix[btn].move(posx, posy)
            matrix[btn].clicked.connect(lambda launch: self.launch(filepath))

        self.initUI()


    def initUI(self):

        self.setGeometry(150, 150, 1250, 650)
        self.setWindowTitle('LinuxLauncher')

        self.show()


    if __name__ == '__main__':

        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())

我知道有一个答案,但我已经回答了几个小时-非常感谢有人可以帮助我摆脱困境-谢谢!

I know there's an answer but I've been at it for hours - I'd appreciate it someone could help me out of this jam - Thanks!

推荐答案

我不了解矩阵是什么类型的结构,但我认为它等同于字典列表.

I do not understand what type of structure is matrix, but I think it is equivalent to a list of dictionaries.

问题是您必须将其作为参数传递给分配它的lambda函数,单击的信号采用布尔值作为参数,该布尔值指示是否选中按钮(默认情况下,此属性处于禁用状态,因此值为false),则必须添加另一个参数.

The problem is that you must pass as an argument to the lambda function assigning it, the clicked signal takes as a parameter a Boolean value that indicates that if the button is checked or not (by default this property is disabled so that this value is false), you must add another parameter.

class App(QMainWindow):
    def launch(self, filepath):
        subprocess.run(filepath)

    def __init__(self):
        super(App, self).__init__()

        matrix = [{"path": "path1", "setIcon": "icon1", "posx": 0, "posy": 0}, 
        {"path": "path2", "setIcon": "icon2", "posx": 0, "posy": 150},
        {"path": "path3", "setIcon": "icon3", "posx": 0, "posy": 300}]

        for value in matrix:

            filepath = value['path']
            icon =  value['setIcon']
            posx = value['posx']
            posy = value['posy']

            btn = QToolButton(self)
            btn.setIcon(QIcon(icon))
            btn.setIconSize(QSize(64, 64))
            btn.resize(100, 100)
            btn.move(posx, posy)
            btn.clicked.connect(lambda checked, arg=filepath: self.launch(arg))

        self.initUI()

    def initUI(self):
        self.setGeometry(150, 150, 1250, 650)
        self.setWindowTitle('LinuxLauncher')
        self.show()

这篇关于在循环中创建PyQt5按钮:所有按钮触发相同的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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