Switching between multiple pages
-
Hi guys, I saw this code online and I'm confused what the pages.ui line is? Can anyone please explain that line to me please? Because the code is switching between just two windows and the pages.ui seem to be a third ui window.Thanks in advance.
from PyQt5 import uic from PyQt5.QtWidgets import QApplication, QMainWindow from first import First from second import Second class MainWindow(QMainWindow): def __init__(self): super().__init__() uic.loadUi('pages.ui', self) self.first = First() self.stackedWidget.addWidget(self.first) self.first.change_btn.clicked.connect(self.go_to_second) self.second = Second() self.stackedWidget.addWidget(self.second) self.second.change_btn.clicked.connect(self.go_to_first) def go_to_first(self): self.stackedWidget.setCurrentIndex(0) def go_to_second(self): self.stackedWidget.setCurrentIndex(1) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_()
from PyQt5 import uic from PyQt5.QtWidgets import QWidget class First(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) uic.loadUi('first.ui', self
from PyQt5 import uic from PyQt5.QtWidgets import QWidget class Second(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) uic.loadUi('second.ui', self
-
Hi guys, I saw this code online and I'm confused what the pages.ui line is? Can anyone please explain that line to me please? Because the code is switching between just two windows and the pages.ui seem to be a third ui window.Thanks in advance.
from PyQt5 import uic from PyQt5.QtWidgets import QApplication, QMainWindow from first import First from second import Second class MainWindow(QMainWindow): def __init__(self): super().__init__() uic.loadUi('pages.ui', self) self.first = First() self.stackedWidget.addWidget(self.first) self.first.change_btn.clicked.connect(self.go_to_second) self.second = Second() self.stackedWidget.addWidget(self.second) self.second.change_btn.clicked.connect(self.go_to_first) def go_to_first(self): self.stackedWidget.setCurrentIndex(0) def go_to_second(self): self.stackedWidget.setCurrentIndex(1) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_()
from PyQt5 import uic from PyQt5.QtWidgets import QWidget class First(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) uic.loadUi('first.ui', self
from PyQt5 import uic from PyQt5.QtWidgets import QWidget class Second(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) uic.loadUi('second.ui', self
@LT-K101
Thepages.ui
is the designer file just for theQStackedWidget
(self.stackedWidget
), e.g. see https://www.tutorialspoint.com/pyqt/pyqt_qstackedwidget.htm? That is the "container" widget which holds references to the individual page widgets and allows switching between which (single) one of them is shown at any time. -
@LT-K101
Thepages.ui
is the designer file just for theQStackedWidget
(self.stackedWidget
), e.g. see https://www.tutorialspoint.com/pyqt/pyqt_qstackedwidget.htm? That is the "container" widget which holds references to the individual page widgets and allows switching between which (single) one of them is shown at any time.