PyQt5:主窗口显示没有任何小部件

时间:2017-11-01 08:12:35

标签: python pyqt5 qmainwindow qmenubar qstatusbar

我正在使用PyQt5来创建我的应用程序。问题是我的窗口出现了,但没有任何小部件(即没有我的Main类,没有任何菜单,工具栏等) - 仍然是空的。我正在使用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_())

.views(main)

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

您可以看到以下结果:

You can see the result bellow:

1 个答案:

答案 0 :(得分:1)

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

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)

这个TerraSoft课程:

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

这个FamilyTreeView课程:

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 EventsTableView(QTableWidget):
    def __init__(self, *args):
        QTableWidget.__init__(self, *args)
        self.setColumnCount(3)
        self.setHorizontalHeaderLabels(('Date', 'Catégorie', 'Description'))