PyQt5

Using Tabs


main index



Using QTabWidget



Figure 1


Listing 1 (tab_pages.py)


from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
  
#===================================================================
# A generic base class from which specialized tab pages are derived. 
class AbstractTabPage(QWidget):
    def __init__(self, panelName):
        QWidget.__init__(self, None)
        self.name = panelName
        vlayout = QVBoxLayout()
        self.setLayout(vlayout)
        self.addWidgets(vlayout)
        vlayout.addStretch()
        self.setFocusPolicy( Qt.StrongFocus )
    #---------------------------------------------------------------
    def addWidgetsPanel(self, parentLayout):
        raise NotImplementedError("addWidgetsPanel() must be implemented")
    #---------------------------------------------------------------
    def doAction(self, parentLayout):
        raise NotImplementedError("doAction must be implemented")
    #---------------------------------------------------------------
    def focusOutEvent(self, event):
        # A derived class should implement this if they wish to
        # respond when a page loses focus.
        pass
    #---------------------------------------------------------------
    def focusInEvent(self, event):
        # A derived class should implement this if they wish to
        # respond when a page gains focus.
        pass
#===================================================================
  
class RedTabPage(AbstractTabPage):
    def __init__(self, panelName):
        AbstractTabPage.__init__(self, panelName)
        # Note: the base class lays out the UI
    #-------------------------------------------------------------
    def addWidgets(self, parentLayout):
        layout = QVBoxLayout()
        self.label = QLabel()
        self.label.setText(self.name)
        self.label.setFont(QFont('Arial', 20, weight=QFont.Bold))
        self.label.setStyleSheet('QLabel {color : red}')
        layout.addWidget(self.label)        
        parentLayout.addLayout(layout)
    #-------------------------------------------------------------
    def doAction(self):
        self.label.setText(self.name + ': doAction() has been called')
    #-------------------------------------------------------------
    def focusOutEvent(self, event):
        self.label.setText(self.name)
        
#-------------------------------------------------------------------
class BlueTabPage(AbstractTabPage):
    def __init__(self, panelName):
        AbstractTabPage.__init__(self, panelName)
        # Note: the base class lays out the UI
    #-------------------------------------------------------------
    def addWidgets(self, parentLayout):
        layout = QVBoxLayout()
        self.label = QLabel()
        self.label.setText(self.name)
        self.label.setFont(QFont("Arial", 20, weight=QFont.Bold))
        self.label.setStyleSheet('QLabel {color : blue}')
        layout.addWidget(self.label)        
        parentLayout.addLayout(layout)
    #-------------------------------------------------------------
    def doAction(self):
        self.label.setText(self.name + ': doAction() has been called')
    #-------------------------------------------------------------
    def focusOutEvent(self, event):
        self.label.setText(self.name)        
#-------------------------------------------------------------------
  


Listing 2 (tabs_ui.py)


# tabs_ui.py
  
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
  
import sys
  
from tab_pages import *
  
class Demo(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        
        # Ensure our window stays in front and give it a title
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.setWindowTitle("Tabs Dialog")
        self.setFixedSize(400, 200)
        
        # Create and assign the main (vertical) layout.
        vlayout = QVBoxLayout()
        self.setLayout(vlayout)    
        
        self.addTabs(vlayout)
        vlayout.addStretch()
        self.addButtonPanel(vlayout)
        self.show()
    #--------------------------------------------------------------------
    def addTabs(self, parentLayout):
        self.tabs = QTabWidget()
        page_1 = RedTabPage("Red")
        page_2 = BlueTabPage("Blue")
  
        self.tabs.addTab(page_1, page_1.name)
        self.tabs.addTab(page_2, page_2.name)
  
        parentLayout.addWidget(self.tabs)
    #--------------------------------------------------------------------
    def addButtonPanel(self, parentLayout):
        self.button = QPushButton("OK")
        self.button.clicked.connect(self.buttonAction)
  
        hlayout = QHBoxLayout()
        hlayout.addStretch()
        hlayout.addWidget(self.button)
        parentLayout.addLayout(hlayout)
    #--------------------------------------------------------------------
    def buttonAction(self):
        page = self.tabs.currentWidget()
        page.doAction()
    #--------------------------------------------------------------------
  
# ========================================================                
if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())


© 2002- Malcolm Kesson. All rights reserved.