Python 3.x 从子菜单添加多个操作
我试图在PyQt4的子菜单中添加另一个操作。我似乎不知道该怎么做。比如说,Python 3.x 从子菜单添加多个操作,python-3.x,pyqt4,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, 5
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()
在上面的代码中,我在子菜单中创建了一个操作。然而,我似乎不知道如何在该子菜单中创建多个操作,这样,当用户进入子菜单“New menu”(新建菜单)时,他们将获得比“Add Action”(添加操作)更多的选项
我尝试创建另一组操作,即:
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)
但是,这只会忽略“添加操作”,而只显示“新操作”
我希望我想做的事情很清楚。如果没有,请说出来,我会尽量说得更清楚。您可以显示您想要的图片。
子菜单=fileMenu.addMenu('newmenu');子菜单.addAction(newAction)
@eyllansc我将尝试添加picture@Rob在那之前,试试ekhumurosolution@Rob将fileMenu.addMenu('newmenu').addAction(newAction)
更改为submenu=fileMenu.addMenu('newmenu');submenu.addAction(newAction)submenu.addAction(AnotherAction)
您必须使用子菜单引用。