1

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()
LW001
  • 2,452
  • 6
  • 27
  • 36
  • 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. – Jonas Vieira de Souza Jul 06 '22 at 17:16
  • The syntax highlighting of an IDE is not perfect nor can foresee the future. Using the signal override prevents the parser to detect the available attributes, but that's just it: it doesn't mean that "it is not assigned to anything", but that the IDE is not able to find the reference due to the dynamic assignment. Remember that IDE highlighting and warnings are *hints*, they don't automatically mean that your code isn't valid or that it won't work. – musicamante Jul 06 '22 at 17:44

1 Answers1

0

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

  • It should be noted that the above code won't do absolutely nothing, at least from the user perspective: not only it clearly ignores the QAction argument, but the message box will not be shown and will be immediately garbage collected at the end of the function, as there's nothing that: 1. shows it; 2. makes it permanent enough to allow any user interaction. The only result will be in creating a no-parent QMessageBox with some aspects set (text, icon and window title), that will be instantly destroyed: from the user perspective, nothing will ever happen *at all*. – musicamante Jul 07 '22 at 04:52
  • You are correct @musicamante I simply answered the question on how to make the connection. I imagine that code will continue to be developed. – Jonas Vieira de Souza Jul 07 '22 at 05:34
  • You're right, but a *good* answer is also responsible of the results of the code that *that answer* provides. We obviously cannot have "control" over what the reader does with our own answers, but it's our responsibility to try to make those answers as insightful as possible, while, obviously, ensuring proper brevity and succinctness. For instance, adding a simple parent argument to the constructor and a basic `msg.exec()` would have made the answer much more complete. You and I might take that for granted, but it's just based on *our* experience. A beginner will probably not. – musicamante Jul 07 '22 at 05:50