我对 PyQt5 完全陌生,我正在尝试开发具有主菜单和子菜单的应用程序,但我不知道该如何开始。我发现的所有示例都使用该弹出窗口作为子菜单。
这是我要做什么的一个例子。请问如何在PyQt5中启动以及使用什么建议?
主菜单在右侧,子菜单在左侧
答案 0 :(得分:0)
要创建主菜单,您可以在图标模式下使用QListWidget
,并且可以使用QStackedLayout
来交换页面:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Page(QtWidgets.QWidget):
clicked = QtCore.pyqtSignal()
def __init__(self, content, parent=None):
super(Page, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
button = QtWidgets.QPushButton("Back")
button.clicked.connect(self.clicked)
lay.addWidget(button)
lay.addWidget(content)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
self.stacked = QtWidgets.QStackedWidget()
lay.addWidget(self.stacked)
self.listWidget = QtWidgets.QListWidget()
self.listWidget.setViewMode(QtWidgets.QListView.IconMode)
self.listWidget.setIconSize(QtCore.QSize(128, 128))
self.listWidget.itemClicked.connect(self.onItemClicked)
self.stacked.addWidget(self.listWidget)
for icon, content in [("alarms.png", QtWidgets.QTextEdit() ),
("fitness.png", QtWidgets.QMdiArea() ),
("navigation.png", QtWidgets.QTableWidget(4, 4) ),
("notifications.png", QtWidgets.QListWidget() )]:
p = Page(content)
p.clicked.connect(lambda: self.stacked.setCurrentIndex(0))
self.addPage(QtGui.QIcon(icon), p)
def addPage(self, icon, widget):
it = QtWidgets.QListWidgetItem()
it.setIcon(icon)
self.listWidget.addItem(it)
self.stacked.addWidget(widget)
it.setData(QtCore.Qt.UserRole, self.listWidget.count())
def onItemClicked(self, it):
ix = it.data(QtCore.Qt.UserRole)
self.stacked.setCurrentIndex(ix)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())