Pyside2 QAction triggers once automatically but not when user clicks the menu

后端 未结 1 863
轻奢々
轻奢々 2021-01-28 14:40

I\'ve created a simple GUI using qt designer and imported it into my python project. The main window comes up, and the menus/buttons are responsive, but I cannot manage to conne

相关标签:
1条回答
  • 2021-01-28 15:06

    You have 2 errors:

    • on_btn_clicked is a method of the class so it must have as the first parameter the instance, that is, self.

    • the connection is with the name of the function without evaluating, in your case the slot on_action_clicked you are evaluating it, so you are getting it printed at the beginning, for it there are 2 possible solutions: use partial or use a lambda function.


        self.btn = self.window.findChild(QPushButton, 'pushButton')
        self.btn.clicked.connect(self.on_btn_clicked)
        self.action_save = self.window.findChild(QAction, 'action_save_state')
        self.action_save.triggered.connect(partial(self.on_action_clicked, 'action clicked'))
        # self.action_save.triggered.connect(lambda: self.on_action_clicked('action clicked')) # use lambda function
        self.window.show()
    
    def on_action_clicked(self, txt):
        print(txt)
    
    def on_btn_clicked(self):
        print('button clicked')
    
    0 讨论(0)
提交回复
热议问题