Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

python - Creating PyQt5 buttons in a loop: all buttons trigger the same callback

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

[Using a dictionary in a for loop to create buttons doesn't work

[QtCore.QObject.connect in a loop only affects the last instance

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...