简体   繁体   中英

PyQt5 GUI program will not show widgets inside the main window

The GUI program I have made will bring up the main window with window title however it is empty and there are no errors in the idle. I have only put in the code for setting up the tab system with a confirm button on the first tab.

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

A QMainWindow is not designed to be used with a Layout. It displays a central widget as it's content. So just replace:

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

with:

self.setCentralWidget(self.tabWidget)

and it should work...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM