PyQt5 GUI程序不会在主窗口中显示小部件

时间:2014-01-20 17:23:26

标签: python pyqt

我制作的GUI程序将打开带窗口标题的主窗口,但它是空的,空闲时没有错误。我只在第一个选项卡上输入了使用确认按钮设置选项卡系统的代码。

import sys
import functions_classes
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):
def __init__(self):
    super(MainWindow,self).__init__()
    self.createUI()

def createUI(self):
    self.setWindowTitle('Equipment Manager 0.1')
    self.tabWidget = QTabWidget()
    #New Data
    self.newDataWidget = QWidget()
    self.newDataLayout = QVBoxLayout()
    self.newFormLayout = QFormLayout()
    self.newConfirmButton = QPushButton('Confirm',self)
    self.newDataLayout.addLayout(self.newFormLayout)
    self.newDataLayout.addWidget(self.newConfirmButton)
    self.newDataWidget.setLayout(self.newDataLayout)
    self.tabWidget.addTab(self.newDataWidget, 'New Data')
    #Update Data
    self.updateDataWidget = QWidget()
    self.updateDataLayout = QVBoxLayout()
    self.updateDataWidget.setLayout(self.updateDataLayout)
    self.tabWidget.addTab(self.updateDataWidget, 'Update Data')
    #Query
    self.queryWidget = QWidget()
    self.queryLayout = QVBoxLayout()
    self.queryWidget.setLayout(self.queryLayout)
    self.tabWidget.addTab(self.queryWidget, 'Query')
    #Main Layout
    self.mainLayout = QVBoxLayout()
    self.mainLayout.addWidget(self.tabWidget)
    self.setLayout(self.mainLayout)

def main():
    mainWindow = MainWindow()
    mainWindow.show()
    mainWindow.raise_()
    application.exec_()

1 个答案:

答案 0 :(得分:2)

QMainWindow并非设计用于布局。它显示了一个中央小部件,因为它是内容。所以只需替换:

#Main Layout
self.mainLayout = QVBoxLayout()
self.mainLayout.addWidget(self.tabWidget)
self.setLayout(self.mainLayout)

使用:

self.setCentralWidget(self.tabWidget)

它应该有用......