0

I am new to PyQt5... Simple question here.

I am using PyQt5 to build a simple application. This application has a Main Window containing a QTabWidget with 3 tabs. Once the application starts, all tab pages are empty and get filled later on. When tab pages are empty, I would still like them to appear as blank pages and extend up to the Main Window edges.

I've been trying to achieve this in two ways: using a layout and using the setGeometry function. Yet the tab pages never extend vertically very far, and horizontally they never go beyond the last tab. See code below.

import sys
from PyQt5.QtWidgets import *


class MainWindow(QWidget):

    def __init__(self):

       super().__init__()

       self.setWindowTitle("Window With Tabs")
       self.setGeometry(50,50,400,400)

       oTabWidget = QTabWidget(self)

       oPage1 = QWidget()
       oLabel1 = QLabel("Hello",self) 
       oVBox1 = QVBoxLayout() 
       oVBox1.addWidget(oLabel1)      
       oPage1.setLayout(oVBox1)

       oPage2 = QWidget()
       oPage2.setGeometry(0,0,400,400)

       oPage3 = QWidget()
       oPage3.setGeometry(0,0,400,400)              

       oTabWidget.addTab(oPage1,"Page1")
       oTabWidget.addTab(oPage2,"Page2")
       oTabWidget.addTab(oPage3,"Page3")      

       self.show()


if __name__ == "__main__":

    app = QApplication(sys.argv)

    oMainwindow = MainWindow()

    sys.exit(app.exec_())

enter image description here

Any idea how to modify the code so the empty pages will extend up to the edges of Main Window ?

martin_0004
  • 357
  • 1
  • 2
  • 15

2 Answers2

0

Set a layout on the main widget:

class MainWindow(QWidget):
    def __init__(self):   
        super().__init__()

        self.setWindowTitle("Window With Tabs")
        self.setGeometry(50,50,400,400)

        layout = QVBoxLayout(self)

        oTabWidget = QTabWidget(self)
        layout.addWidget(oTabWidget)

The setGeometry calls on the other widgets are redundant.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0
import sys
from PyQt5.QtWidgets import *


class MainWindow(QWidget):
    # window object
    def __init__(self):
       super().__init__()
       self.initGUI() # call custom code

    def initGUI(self):   
       self.setWindowTitle("Window With Tabs") # window...
       self.setGeometry(50,50,400,400)         #...properties   
       TabW=self.createTabs()                  # a custom-tab object
       layout = QVBoxLayout(self)              # main window layout   
       layout.addWidget(TabW)                  #populate layout with Tab object
       self.show()                             # display window   

    def createTabs(self):            # create and return Tab object
       oPage1 = QWidget()            # tabs...
       oPage2 = QWidget()
       oPage3 = QWidget()

       oTabWidget = QTabWidget()    # Tabobject

       oTabWidget.addTab(oPage1,"Page1") # populate tab object...
       oTabWidget.addTab(oPage2,"Page2")
       oTabWidget.addTab(oPage3,"Page3") 

       return   oTabWidget              # return tab object  

if __name__ == "__main__":             # Rest is History!

    app = QApplication(sys.argv)
    oMainwindow = MainWindow()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241