pythonpyqtqaction

Is there a way to connect the "RPG" menu with the "clickMSG" function?


I want to connect the "RPG" menu with the "clickMSG" function.

I don't get an error when I run the code, but the connect is colored in white, therefor is not assigned to anything. Here is the code:

class gui(QMainWindow): 
   def __init__(self, parent = None): 
      super(gui, self).__init__(parent)
      layout = QHBoxLayout()
      menu = self.menuBar()  
      file = menu.addMenu("New Account") 
      file.addAction("file") 

      file2 = menu.addMenu('RGP')   
      rpg = QAction("RGP") 
      file2.triggered[QAction].connect(self.clickMSG)
      
   def clickMSG(self): 
      msg = QMessageBox()  
      msg.setText("This is your RGP") 
      msg.setWindowTitle("This is an RGP") 
      msg.setIcon(QMessageBox.Information)
      pwo = PasswordGenerator()  
      passwrd = pwo.shuffle_password(string.ascii_letters, 10)   
        
def main(): 
   app = QApplication(sys.argv)  
   ex = gui()  
   ex.show()
   
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

Solution

  • You should have made the second menubar button just like you made the first.

    Use the addAction() method on the item provided with the addMenu() method.

    import sys
    from PyQt5.QtWidgets import QMessageBox, QAction, QHBoxLayout, QApplication, QWidget, QPushButton, QVBoxLayout, QMainWindow
    from PyQt5.QtCore import pyqtProperty
    
    class gui(QMainWindow): 
        def __init__(self, parent = None): 
            super(gui, self).__init__(parent)
            layout = QHBoxLayout()
            menu = self.menuBar()  
            file = menu.addMenu("New Account") 
            file.addAction("file") 
    
            file2 = menu.addMenu('RGP-1')
            #rpg = QAction("RGP")
            file2.addAction("RGP-2") 
            file2.triggered[QAction].connect(self.clickMSG)
    
        def clickMSG(self):
            print('here !')
            msg = QMessageBox()  
            msg.setText("This is your RGP") 
            msg.setWindowTitle("This is an RGP") 
            msg.setIcon(QMessageBox.Information)
            #pwo = PasswordGenerator()  
            #passwrd = pwo.shuffle_password(string.ascii_letters, 10)   
        
    def main(): 
        app = QApplication(sys.argv)  
        ex = gui()  
        ex.show()
    
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
       main()
    

    Hope this helps