我想将“RPG”菜单与“clickMSG”功能连接起来。
运行代码时没有收到错误,但连接颜色为白色,因此未分配给任何内容。 这是代码:
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()
您应该像制作第一个菜单栏按钮一样制作第二个菜单栏按钮。
对通过 addMenu() 方法提供的项目使用 addAction() 方法。
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()
希望这有帮助