Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Multiple page UI within same window UI PyQt

from PyQt5.QtWidgets import (QWidget, QPushButton,
    QHBoxLayout, QVBoxLayout, QApplication, QStackedWidget, QLabel)

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()
        self.stacked_widget.currentChanged.connect(self.set_button_state)
        self.next_button.clicked.connect(self.next_page)
        self.prev_button.clicked.connect(self.prev_page)


    def initUI(self):

        self.next_button = QPushButton('Next')
        self.prev_button = QPushButton('Previous')
        self.next_button.setEnabled(False)
        self.prev_button.setEnabled(False)
        self.stacked_widget = QStackedWidget()

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(self.prev_button)
        hbox.addWidget(self.next_button)

        vbox = QVBoxLayout()
        vbox.addWidget(self.stacked_widget)
        vbox.addLayout(hbox)

        self.setLayout(vbox)


    def set_button_state(self, index):
        self.prev_button.setEnabled(index > 0)
        n_pages = len(self.stacked_widget)
        self.next_button.setEnabled( index % n_pages < n_pages - 1)

    def insert_page(self, widget, index=-1):
        self.stacked_widget.insertWidget(index, widget)
        self.set_button_state(self.stacked_widget.currentIndex())

    def next_page(self):
        new_index = self.stacked_widget.currentIndex()+1
        if new_index < len(self.stacked_widget):
            self.stacked_widget.setCurrentIndex(new_index)

    def prev_page(self):
        new_index = self.stacked_widget.currentIndex()-1
        if new_index >= 0:
            self.stacked_widget.setCurrentIndex(new_index)

if __name__ == '__main__':

    app = QApplication([])
    ex = Example()
    for i in range(5):
        ex.insert_page(QLabel(f'This is page {i+1}'))
    ex.resize(400,300)
    ex.show()
    app.exec()
Comment

PREVIOUS NEXT
Code Example
Python :: convert only time to unix timestamp python 
Python :: pyqt fixed window size 
Python :: pandas form multiindex to column 
Python :: b-spline quantile regression with statsmodels 
Python :: pandas read sql generator to dataframe 
Python :: exception: python in worker has different version 3.7 than that in driver 3.8, pyspark cannot run with different minor versions. please check environment variables pyspark_python and pyspark_driver_python are correctly set. 
Python :: iif python 
Python :: dataset ( data.h5 ) containing cat or non-cat images download 
Python :: openCV error [WARN:0] terminating async callback 
Python :: python deep setter 
Python :: #finding the differences between setA and SetB: 
Python :: text file sort by first item in each row 
Python :: remove color from shapefile python 
Python :: Filter dataarray 
Python :: ploting bargraph with value_counts(with title x and y label and name angle) 
Python :: python function changing arguments 
Python :: counter vectriozer in python 
Python :: integer to binary python 16 bit 
Python :: python 2.0 
Python :: Another example: using a colorbar to show bar height 
Python :: python return true for list comprehension 
Python :: ValueError: unknown is not supported in sklearn.RFECV 
Python :: how to import pil in spyder 
Python :: python in a nutshell 
Python :: python how to get variable value in dict 
Python :: pandas resample fill missing values 
Python :: python fibonacci numbers 
Python :: python mypy cast 
Python :: awk extract one file from another file 
Python :: bs.newtag() inner html 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =