我正在使用 PyQt5 创建我的应用程序。问题是我的窗口出现了,但没有任何小部件(即没有我的Main
类,没有任何菜单、工具栏等) - 仍然是空的。我正在使用 Python-3.6 和 PyQt5,Windows 10 环境。
这是我的代码:
应用程序.py:
#!/usr/bin/env python
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, qApp, QWidget, QMainWindow, QGridLayout, QMenuBar, QAction, QToolBar, QStatusBar
from views import Main
class TerraSoft(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('TerraSoft')
self.setWindowState(Qt.WindowMaximized)
# init menu
menubar = QMenuBar()
fileMenu = menubar.addMenu('File')
#init toolbar
exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(qApp.quit)
toolbar = QToolBar()
# load ihm
main = Main()
centWidget = QWidget()
# widget centralizer
window = QGridLayout()
window.addWidget(main, 0, 0)
centWidget.setLayout(window)
self.setCentralWidget(centWidget)
#status bar
statusBar = QStatusBar().showMessage('Bienvenue dans TerraSoft')
# show ihm
self.show()
main.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TerraSoft()
ex.show()
sys.exit(app.exec_())
.views(主要)
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, qApp, QAction, QSplitter, QMenuBar, QToolBar, QGridLayout, QStatusBar
from Family.views import FamilyTreeView
from Specie.views import EventsTableView
class Main(QWidget):
"""description of class"""
def __init__(self, *args):
QWidget.__init__(self, *args)
# init elements of widget
familyTreeView = FamilyTreeView(self)
eventsTableView = EventsTableView(self)
#init splitters
HSplitter = QSplitter(Qt.Horizontal)
VSplitter = QSplitter(Qt.Vertical)
# add elements to splitters
HSplitter.addWidget(familyTreeView)
HSplitter.addWidget(eventsTableView)
grid = QGridLayout()
grid.addWidget(HSplitter)
我的模块family.views
(我想在未来添加 .models,但一件接一件):
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTreeView
families = [
("Craspedocephalus", [
("puniceus", []),
("trigonocephalus", [])
]),
("Trimeresurus", [
("albolabris", [])
]),
("Elapidé", [])
]
class FamilyTreeView(QWidget):
"""description of class"""
def __init__(self, *args):
QWidget.__init__(self, *args)
self.familyList = QTreeView()
self.familyList.setMaximumWidth(300)
self.model = QStandardItemModel()
self.addItems(self.model, families)
self.familyList.setModel(self.model)
self.model.setHorizontalHeaderLabels([self.tr("Familles")])
def addItems(self, parent, elements):
for text, children in elements:
item = QStandardItem(text)
parent.appendRow(item)
if children:
self.addItems(item, children)
和我的模块specie.views
:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTreeView
class EventsTableView(QTableWidget):
"""description of class"""
def __init__(self, *args):
QTableWidget.__init__(self, *args)
self.eventsTable = QTableWidget()
self.eventsTable.setColumnCount(3)
self.eventsTable.setHorizontalHeaderLabels(('Date', 'Catégorie', 'Description'))
你可以看到下面的结果: