PyQt5: main window 显示没有任何小部件

PyQt5: main window shows without any widgets

我正在使用 PyQt5 创建我的应用程序。问题是我的 window 出现了,但没有任何小部件(即没有我的 Main class,没有任何菜单、工具栏等)- 仍然是空的。我正在使用 Python-3.6 和 PyQt5,Windows 10 环境。

这是我的代码:

app.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_())

.观看次数(主要)

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'))

您可以看到下面的结果:

要修复您的示例,请使用此 Main class:

class Main(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        familyTreeView = FamilyTreeView(self)
        eventsTableView = EventsTableView(self)

        HSplitter = QSplitter(Qt.Horizontal)

        HSplitter.addWidget(familyTreeView)
        HSplitter.addWidget(eventsTableView)
        HSplitter.setStretchFactor(1, 1)

        grid = QGridLayout()
        grid.addWidget(HSplitter)
        self.setLayout(grid)

这个TerraSoftclass:

class TerraSoft(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle('TerraSoft')
        self.setWindowState(Qt.WindowMaximized)

        exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.triggered.connect(qApp.quit)

        fileMenu = self.menuBar().addMenu('File')
        fileMenu.addAction(exitAct)

        toolbar = self.addToolBar('Main')
        toolbar.addAction(exitAct)

        main = Main()
        self.setCentralWidget(main)

        self.statusBar().showMessage('Bienvenue dans TerraSoft')

这个FamilyTreeViewclass:

class FamilyTreeView(QTreeView):
    def __init__(self, *args):
        QTreeView.__init__(self, *args)
        self.setMaximumWidth(300)
        self.setModel(QStandardItemModel(self))
        self.model().setHorizontalHeaderLabels([self.tr("Familles")])
        self.addItems(self.model(), families)

    def addItems(self, parent, elements):
        for text, children in elements:
            item = QStandardItem(text)
            parent.appendRow(item)
            if children:
                self.addItems(item, children)

和这个 EventsTableView class:

class EventsTableView(QTableWidget):
    def __init__(self, *args):
        QTableWidget.__init__(self, *args)
        self.setColumnCount(3)
        self.setHorizontalHeaderLabels(('Date', 'Catégorie', 'Description'))