0

I want to change dispaly on button click. my current code:

import sys
from PyQt5 import QtWidgets, uic

Ui_bankApp, _ = uic.loadUiType("bankApp.ui")
Ui_Borrow, _ = uic.loadUiType("Borrow.ui")
Ui_Login, _ = uic.loadUiType("login.ui")
Ui_Pay, _ = uic.loadUiType("Pay.ui")

class Borrow(object):
    def setupUI(self, Ui):
        uic.loadUi('Borrow.ui', self)

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        self.bankApp()

    def bankApp(self):
        print("bankApp")
        self.ui = Ui_bankApp()
        self.ui.setupUi(self)
        self.hide()
        self.show()
        self.button = self.findChild(QtWidgets.QPushButton, 'pushButton_2') 
        self.button.clicked.connect(self.Borrow)


    def Borrow(self):
        print("Borrow")
        self.ui1 = Ui_Borrow()
        self.ui1.setupUi(self)
        self.button1 = self.findChild(QtWidgets.QPushButton, 'pushButton_2') 
        self.button1.clicked.connect(self.bankApp)


    def Login(selft):
        self.ui = Ui_Login()
        self.ui.setupUi(self)

    def Pay():
        self.ui = Ui_Pay()
        self.ui.setupUi(self)


app = QtWidgets.QApplication(sys.argv)
window = Ui()
window.show()
app.exec_()

my two ui files look like: bankApp.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>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>190</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Pay</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>610</x>
      <y>180</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Borrow</string>
    </property>
   </widget>
   <widget class="QLCDNumber" name="lcdNumber">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>30</y>
      <width>321</width>
      <height>91</height>
     </rect>
    </property>
    <property name="intValue" stdset="0">
     <number>1000</number>
    </property>
   </widget>
   <widget class="QLCDNumber" name="lcdNumber_2">
    <property name="geometry">
     <rect>
      <x>600</x>
      <y>270</y>
      <width>131</width>
      <height>51</height>
     </rect>
    </property>
   </widget>
   <widget class="QLCDNumber" name="lcdNumber_3">
    <property name="geometry">
     <rect>
      <x>600</x>
      <y>370</y>
      <width>131</width>
      <height>51</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>600</x>
      <y>230</y>
      <width>91</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Spent</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>600</x>
      <y>340</y>
      <width>47</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Earned</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>250</y>
      <width>47</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Debt</string>
    </property>
   </widget>
   <widget class="QLCDNumber" name="lcdNumber_4">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>270</y>
      <width>141</width>
      <height>51</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Borrow:

<?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>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Borrow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>20</y>
      <width>51</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Back</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>290</y>
      <width>281</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>Borrow</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>200</y>
      <width>281</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>Amount</string>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

so i want to switch to borrow page when i click the borrow button in the bankApp then i want to switch to bankApp page when i press the back button. but when i click the back button on the borrow page it does not go back to the bankApp page

1 Answer 1

1

You could consider using a QStackedWidget to switch between the layouts instead of changing the ui of the on the fly. For example,

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.stacked_widget = QtWidgets.QStackedWidget()
        self.bankApp()
        self.Borrow()
        self.setCentralWidget(self.stacked_widget)
        self.resize(800,600)

    def bankApp(self):
        self.bank_widget = QtWidgets.QMainWindow()
        Ui_bankApp().setupUi(self.bank_widget)
        self.stacked_widget.addWidget(self.bank_widget)
        self.button = self.bank_widget.findChild(QtWidgets.QPushButton, 'pushButton_2')
        self.button.clicked.connect(lambda : self.goto_page(self.borrow_widget))

    def Borrow(self):
        self.borrow_widget = QtWidgets.QMainWindow()
        Ui_Borrow().setupUi(self.borrow_widget)
        self.stacked_widget.addWidget(self.borrow_widget)
        self.button1 = self.borrow_widget.findChild(QtWidgets.QPushButton, 'pushButton_2')
        self.button1.clicked.connect(lambda : self.goto_page(self.bank_widget))

    def goto_page(self, widget):
        index = self.stacked_widget.indexOf(widget)
        print(index, widget)
        if index >= 0:
            self.stacked_widget.setCurrentIndex(index)
Sign up to request clarification or add additional context in comments.

2 Comments

How can i add variable to self.goto_page(self.borrow_widget))
like self.borrow_widget(var,var2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.