从子菜单添加多个动作

时间:2018-07-17 16:56:38

标签: python-3.x pyqt4

我正在尝试在PyQt4的子菜单中添加其他操作。我似乎无法弄清楚该怎么做。例如,

from PyQt4 import QtGui
import sys


class Window(QtGui.QMainWindow):

  def __init__(self):
    super(Window, self).__init__()  #Returns the parent object or the QMainWindow object
    self.setGeometry(50, 50, 500, 300)
    self.setWindowTitle("Testing")

    newAction = QtGui.QAction("&Add Action", self)
    newAction.triggered.connect(self.new_action)

    mainMenu = self.menuBar()

    fileMenu = mainMenu.addMenu('&File')
    fileMenu.addMenu('New Menu').addAction(newAction)

  def home(self):
    self.show()

  def new_action(self):
    print("Made it")


    pass


def run():
  app = QtGui.QApplication(sys.argv)
  GUI = Window()
  sys.exit(app.exec_())


run()
在上面的代码中,

我已经在子菜单中创建了一个动作。但是,我似乎无法弄清楚如何在该子菜单中创建多个动作,这样,当用户进入子菜单“新建菜单”时,除了“添加动作”之外,还将为他们提供更多选项。

我尝试创建另一组动作,即:

newAction = QtGui.QAction("&Add Action", self)
    newAction.triggered.connect(self.new_action)

    AnotherAction = QtGui.QAction("&Add Action", self)
    AnotherAction.triggered.connect(self.new_action)

    mainMenu = self.menuBar()

    fileMenu = mainMenu.addMenu('&File')
    fileMenu.addMenu('New Menu').addAction(newAction).addAction(AnotherAction)

但这给了我一个错误:

fileMenu.addMenu('New Menu').addAction(newAction).addAction(AnotherAction)
AttributeError: 'NoneType' object has no attribute 'addAction'

我也尝试过:

newAction = QtGui.QAction("&Add Action", self)
    newAction.triggered.connect(self.new_action)

    newAction = QtGui.QAction("&New Action", self)
    newAction.triggered.connect(self.new_action)

    mainMenu = self.menuBar()

    fileMenu = mainMenu.addMenu('&File')
    fileMenu.addMenu('New Menu').addAction(newAction)

但是,这只是忽略“添加操作”,而仅显示“新操作”。

我希望很清楚我要做什么。如果没有,请这样说,我将尝试使其更加清晰。

0 个答案:

没有答案
相关问题