Python Forum
How to go to next page in PyQt GUI?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to go to next page in PyQt GUI?
#1
Hi,

I am new to python. I am developing an application in python using PyQt4. I want a multipage application. I wrote one code this I need help how can I code when I press the next button on the first page it should go to the next page with different widgets.

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(0, 0, 800, 480)
        self.setWindowTitle("Femikare")
        self.home()
        
    def home(self):
        label1 = QtGui.QLabel(self)
        label1.setText("Please Select One Product")
        label1.setStyleSheet("font: bold 50px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        label1.resize(800,40)
        label1.move(0,0)        
        
        #qApp->setStyleSheet("QRadioButton { background-color: white; background-position: center}");
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/1.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "1"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(0,40)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l1 = QtGui.QLabel(self)
        p1 = 20
        l1.setText("Price: %d" %p1)
        l1.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l1.resize(200,20)
        l1.move(0,210)
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/2.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "2"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(200,40)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l2 = QtGui.QLabel(self)
        p2 = 20
        l2.setText("Price: %d" %p2)
        l2.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l2.resize(200,20)
        l2.move(200,210)
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/3.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "3"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(400,40)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l3 = QtGui.QLabel(self)
        p3 = 20
        l3.setText("Price: %d" %p3)
        l3.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l3.resize(200,20)
        l3.move(400,210)
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/4.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "4"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(600,40)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l4 = QtGui.QLabel(self)
        p4 = 20
        l4.setText("Price: %d" %p4)
        l4.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l4.resize(200,20)
        l4.move(600,210)
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/5.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "5"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(0,240)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l5 = QtGui.QLabel(self)
        p5 = 20
        l5.setText("Price: %d" %p5)
        l5.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l5.resize(200,20)
        l5.move(0,410)
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/6.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "6"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(200,240)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l6 = QtGui.QLabel(self)
        p6 = 20
        l6.setText("Price: %d" %p6)
        l6.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l6.resize(200,20)
        l6.move(200,410)
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/7.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "7"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(400,240)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l7 = QtGui.QLabel(self)
        p7 = 20
        l7.setText("Price: %d" %p7)
        l7.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l7.resize(200,20)
        l7.move(400,410)
        
        rdoProduct = QtGui.QRadioButton(self)
        rdoProduct.setIcon(QtGui.QIcon("images/8.png"))
        rdoProduct.setIconSize(QtCore.QSize(180,180))
        rdoProduct.productId = "8"
        rdoProduct.toggled.connect(self.test)
        rdoProduct.resize(200,200)
        rdoProduct.move(600,240)
        rdoProduct.setStyleSheet('QRadioButton::unchecked{background-color: white; width: 0px; height: 0px;}' 'QRadioButton::checked{background-color: pink; width: 0px; height: 0px;}' 'QRadioButton::indicator{width: 0px; height: 0px;}')
        l8 = QtGui.QLabel(self)
        p8 = 20
        l8.setText("Price: %d" %p8)
        l8.setStyleSheet("font: bold 20px;" "color: rgb(255, 0, 127);" "background-color: white;" "qproperty-alignment: AlignCenter;")
        l8.resize(200,20)
        l8.move(600,410)
        
        btn = QtGui.QPushButton("NEXT", self)
        #btn.setIcon(QtGui.QIcon("images/Next.png"))
        #btn.setIconSize(QtCore.QSize(40,40))
        btn.setStyleSheet("font: bold 20px;" "color: rgb(0, 0, 0);")
        btn.clicked.connect(self.test)
        btn.resize(400,40)
        btn.move(400,440)
        
        btn = QtGui.QPushButton("CANCEL",self)
        #btn.setIcon(QtGui.QIcon("images/Cancel.png"))
        #btn.setIconSize(QtCore.QSize(40,40))
        btn.setStyleSheet("font: bold 20px;" "color: rgb(0, 0, 0);")
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        btn.resize(400,40)
        btn.move(0,440)
        self.show()
        
    def test(self):
        rdoProduct = self.sender()
        if rdoProduct.isChecked():
            v = rdoProduct.productId
            print(v)
                   
def run():        
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()
Reply
#2
Use a QStackedWidget:


foo.py

#!/usr/bin/python3
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "/foo.ui", self)
        self.ui.listWidget.itemSelectionChanged.connect(self._pageUpdate)
        self.show()

    def _pageUpdate(self):
        index = self.ui.listWidget.currentRow()
        self.ui.stackedWidget.setCurrentIndex(index)

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
foo.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>463</width>
    <height>216</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QListWidget" name="listWidget">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
        <horstretch>1</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <item>
       <property name="text">
        <string>Page1</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string>Page2</string>
       </property>
      </item>
     </widget>
    </item>
    <item>
     <widget class="QStackedWidget" name="stackedWidget">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>2</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="currentIndex">
       <number>0</number>
      </property>
      <widget class="QWidget" name="page">
       <layout class="QHBoxLayout" name="horizontalLayout_3">
        <item>
         <widget class="QLabel" name="label">
          <property name="text">
           <string>Page 1</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QWidget" name="page_2">
       <layout class="QHBoxLayout" name="horizontalLayout_2">
        <item>
         <widget class="QLabel" name="label_2">
          <property name="text">
           <string>Page 2</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>463</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
Reply
#3
Thank you alfalfa. You provided me a PyQt5 creator snippet but I am writing directly in python so if I can find some example in python itself then it would be great.
Reply
#4
Here is an in-code example of using a Stacked-Widget. I used it to help show someone how to keep a button from one page to the next and how to pass information between pages but it has the basics for the usage of a stacked widget

from sys  import exit as sysExit

from PyQt5.QtCore    import QSize
#Qt Widget Containers
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QFrame, QVBoxLayout, QHBoxLayout
from PyQt5.QtWidgets import QStackedWidget, QDockWidget, QDialog
#Qt Widget Objects
from PyQt5.QtWidgets import QPushButton, QTextEdit, QToolButton, QAction

class Win1Disply(QFrame):
    def __init__(self, parent):
        QFrame.__init__(self)

        self.setFrameShape(QFrame.StyledPanel)
        self.setLineWidth(0.2)
        # -------
        self.Cntnr = QVBoxLayout()
        self.Cntnr.addWidget(QTextEdit('This is Window 1 with whatever contents you want'))
        self.Win1Btn = QPushButton('>>')
        self.Win1Btn.clicked.connect(parent.RightArrow)
        self.Cntnr.addWidget(self.Win1Btn)
        self.Cntnr.addStretch(1)
        # -------
        self.setLayout(self.Cntnr)

class Win2Disply(QFrame):
    def __init__(self, parent):
        QFrame.__init__(self)

        self.setFrameShape(QFrame.StyledPanel)
        self.setLineWidth(0.2)
        # -------
        self.Cntnr = QVBoxLayout()
        self.Cntnr.addWidget(QTextEdit('This is Window 2 with whatever contents you want'))
        self.Win1Btn = QPushButton('>>')
        self.Win1Btn.clicked.connect(parent.RightArrow)
        self.Cntnr.addWidget(self.Win1Btn)
        self.Cntnr.addStretch(1)
        # -------
        self.setLayout(self.Cntnr)

class OptionButtons(QToolButton):
# Class OptionButtons ("Text", Connector) inherits from QToolButton
    def __init__(self, Text, Connector):
        QToolButton.__init__(self)

        self.setText(Text)
        self.setStyleSheet("font: bold;color: blue;height: 55px;width: 55px;")
        self.setIconSize(QSize(32,32))
        self.clicked.connect(Connector)

############################## Settings Class ##############################
class OptionSettings(QDialog):
    def __init__(self, parent):
        QDialog.__init__(self, parent)

        line = QFrame()
        line.setFrameShape(QFrame.HLine)
        line.setFrameShadow(QFrame.Sunken)

        self.btnWin1 = OptionButtons('Win One', self.ShowWindow1)
        self.btnWin2 = OptionButtons('Win Two', self.ShowWindow2)
      # Vertical Box for Buttons *************************************
        self.UpLeft  = QVBoxLayout()
        self.UpLeft.addWidget(self.btnWin1)
        self.UpLeft.addWidget(self.btnWin2)
        self.UpLeft.addStretch(1)
  # Display Area on Right
      # Widget Flip Display ******************************************
        self.UpRite   = QHBoxLayout()
        self.Contents = QStackedWidget()
        self.Contents.addWidget(QTextEdit('Nothing Selected'))
        self.Contents.addWidget(Win1Disply(self))
        self.Contents.addWidget(Win2Disply(self))
        self.Contents.addWidget(QTextEdit('Settings Saved'))
        self.Contents.setCurrentIndex(0)
        self.UpRite.addWidget(self.Contents)

  # Button and Display Area on Top
        self.Upper = QHBoxLayout()
        self.Upper.addLayout(self.UpLeft)
        self.Upper.addLayout(self.UpRite)
  # Save and Cancel Area on Bottom
        self.btnSave = QPushButton("Save")
        self.btnSave.clicked.connect(self.SaveSettings)
        self.btnCncl = QPushButton("Cancel")
        self.btnCncl.clicked.connect(self.close)
        self.Lower   = QHBoxLayout()
        self.Lower.addStretch(1)
        self.Lower.addWidget(self.btnSave)
        self.Lower.addWidget(self.btnCncl)
  # Entire Options Window Layout
        self.OuterBox = QVBoxLayout()
        self.OuterBox.addLayout(self.Upper)
        self.OuterBox.addLayout(self.Lower)
        self.setLayout(self.OuterBox)
        self.setWindowTitle('Settings')
        #Geometry(Left, Top, Width, Hight)
        self.setGeometry(250, 250, 550, 450)
        self.setModal(True)
        self.exec()

    def ShowWindow1(self):
        self.Contents.setCurrentIndex(1)

    def ShowWindow2(self):
        self.Contents.setCurrentIndex(2)

    def SaveSettings(self):
        self.Contents.setCurrentIndex(3)
    
    def RightArrow(self):
        if self.Contents.currentIndex() == 1:
           self.Contents.setCurrentIndex(2)
        else:
           self.Contents.setCurrentIndex(1)

class CenterPanel(QWidget):
    def __init__(self, MainWin):
        QWidget.__init__(self)

        CntrPane = QTextEdit('Center Panel is Placed Here')

        hbox = QHBoxLayout(self)
        hbox.addWidget(CntrPane)

        self.setLayout(hbox)

class MenuToolBar(QDockWidget):
    def __init__(self, MainWin):
        QDockWidget.__init__(self)
        self.MainWin = MainWin
        self.MainMenu = MainWin.menuBar()

        self.WndowMenu  = self.MainMenu.addMenu('Windows')

        self.OptnAct = QAction('Options', self)
        self.OptnAct.setStatusTip('Open the Options Window')
        self.OptnAct.triggered.connect(MainWin.ShowOptions)

        self.WndowMenu.addAction(self.OptnAct)

        self.InitToolBar(MainWin)

    def InitToolBar(self, MainWin):
        self.mainToolBar = MainWin.addToolBar("Quick Access")

        self.mainToolBar.addAction(self.OptnAct)

class UI_MainWindow(QMainWindow):
    def __init__(self):
        super(UI_MainWindow, self).__init__()
        self.setWindowTitle('Main Window')

      # Left, Top, Width, Height
        self.setGeometry(200, 200, 550, 550)
 
        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)

        self.MenuToolBar = MenuToolBar(self)

    def ShowOptions(self):
        self.Options = OptionSettings(self)

if __name__ == '__main__':
    MainApp = QApplication([])

    MainGui = UI_MainWindow()
    MainGui.show()

    sysExit(MainApp.exec_())
Reply
#5
Here is the same example, but with the ui file translated to python. It is generated by uic so you might want to clean it up a bit.

foo.py

#!/usr/bin/python3
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

import gui
LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = gui.Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.listWidget.itemSelectionChanged.connect(self._pageUpdate)
        self.show()

    def _pageUpdate(self):
        index = self.ui.listWidget.currentRow()
        self.ui.stackedWidget.setCurrentIndex(index)

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
gui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/tmp/gui.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(463, 216)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.listWidget = QtWidgets.QListWidget(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.listWidget.sizePolicy().hasHeightForWidth())
        self.listWidget.setSizePolicy(sizePolicy)
        self.listWidget.setObjectName("listWidget")
        item = QtWidgets.QListWidgetItem()
        self.listWidget.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listWidget.addItem(item)
        self.horizontalLayout.addWidget(self.listWidget)
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(2)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.stackedWidget.sizePolicy().hasHeightForWidth())
        self.stackedWidget.setSizePolicy(sizePolicy)
        self.stackedWidget.setObjectName("stackedWidget")
        self.page = QtWidgets.QWidget()
        self.page.setObjectName("page")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.page)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label = QtWidgets.QLabel(self.page)
        self.label.setObjectName("label")
        self.horizontalLayout_3.addWidget(self.label)
        self.stackedWidget.addWidget(self.page)
        self.page_2 = QtWidgets.QWidget()
        self.page_2.setObjectName("page_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.page_2)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.page_2)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.stackedWidget.addWidget(self.page_2)
        self.horizontalLayout.addWidget(self.stackedWidget)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 463, 24))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        __sortingEnabled = self.listWidget.isSortingEnabled()
        self.listWidget.setSortingEnabled(False)
        item = self.listWidget.item(0)
        item.setText(_translate("MainWindow", "Page1"))
        item = self.listWidget.item(1)
        item.setText(_translate("MainWindow", "Page2"))
        self.listWidget.setSortingEnabled(__sortingEnabled)
        self.label.setText(_translate("MainWindow", "Page 1"))
        self.label_2.setText(_translate("MainWindow", "Page 2"))
Reply
#6
Hello, thank you for your reply it helped me. I wrote my code like this, not exactly but like this structure.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class stackedExample(QWidget):

   def __init__(self):
      super(stackedExample, self).__init__()
		self.leftlist = QListWidget ()
      self.leftlist.insertItem (0, 'Contact' )
      self.leftlist.insertItem (1, 'Personal' )
      self.leftlist.insertItem (2, 'Educational' )
		
      self.stack1 = QWidget()
      self.stack2 = QWidget()
      self.stack3 = QWidget()
		
      self.stack1UI()
      self.stack2UI()
      self.stack3UI()
		
      self.Stack = QStackedWidget (self)
      self.Stack.addWidget (self.stack1)
      self.Stack.addWidget (self.stack2)
      self.Stack.addWidget (self.stack3)
		
      hbox = QHBoxLayout(self)
      hbox.addWidget(self.leftlist)
      hbox.addWidget(self.Stack)

      self.setLayout(hbox)
      self.leftlist.currentRowChanged.connect(self.display)
      self.setGeometry(300, 50, 10,10)
      self.setWindowTitle('StackedWidget demo')
      self.show()
		
   def stack1UI(self):
      layout = QFormLayout()
      layout.addRow("Name",QLineEdit())
      layout.addRow("Address",QLineEdit())
      #self.setTabText(0,"Contact Details")
      self.stack1.setLayout(layout)
		
   def stack2UI(self):
      layout = QFormLayout()
      sex = QHBoxLayout()
      sex.addWidget(QRadioButton("Male"))
      sex.addWidget(QRadioButton("Female"))
      layout.addRow(QLabel("Sex"),sex)
      layout.addRow("Date of Birth",QLineEdit())
		
      self.stack2.setLayout(layout)
		
   def stack3UI(self):
      layout = QHBoxLayout()
      layout.addWidget(QLabel("subjects"))
      layout.addWidget(QCheckBox("Physics"))
      layout.addWidget(QCheckBox("Maths"))
      self.stack3.setLayout(layout)
		
   def display(self,i):
      self.Stack.setCurrentIndex(i)
		
def main():
   app = QApplication(sys.argv)
   ex = stackedExample()
   sys.exit(app.exec_())
	
if __name__ == '__main__':
   main()
Now I want to pass a variable value from one window to another window so I can use that value there. For example, if I have a variable x = 10 in stack1UI, I want to pass this x value to stack2UI and want to do y = x*10.
Reply
#7
Change this :
      self.stack1UI()
      self.stack2UI()
      self.stack3UI()
To this:
      self.stack1UI(self)
      self.stack2UI(self)
      self.stack3UI(self)
Add this:
   def stack1UI(self, parent):
       self.Parent = parent
Then all you have to do is:
   self.Parent.Value = X
Or if your Parent needs to adjust its child
   self.Stack1.Value = X
Those QWidgets would have however been better handled by making each one their own Class
   def stack1UI(QWidget):
      def __init__(self, parent):
         QWidget.__init__(self)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020