2014-01-20 75 views
1

我所做的GUI程序將帶有窗口標題的主窗口,但是它是空的,並且空閒時沒有錯誤。我只在第一個選項卡上放置了用於確認按鈕的代碼。PyQt5 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_() 

回答

2

A QMainWindow不適用於Layout。它顯示一箇中央小部件,因爲它的內容。因此,只需更換:

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

有:

self.setCentralWidget(self.tabWidget) 

,它應該工作...