Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

python - How to change UI in same window using PyQt5?

I'm just getting started with PyQt5. I have been trying to accomplish a seemingly very simple task but haven't been able to get enough info about it. After a fair bit of googling I have been able to get one window to close and another to launch with the other UI loaded but that's not what I want to do here.

I want to switch the UI in the same window. I am loading the UI files as global variables in my python file where I have 2 classes for each UI. When I click a particular button in one UI, I want to switch to the other UI in the same window. Below is a sample of the code:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from PyQt5.uic import loadUiType
import os

about_company_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'about_company.ui'))
intern_placement_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'intern_placement.ui'))


class InternPlacement(QMainWindow, intern_placement_ui):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.intern_pushButton.clicked.connect(self.change)

    def change(self):
        self.about_company = AboutCompany()
        self.about_company.show()
        self.close()


class AboutCompany(QMainWindow, about_company_ui):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = InternPlacement()
    window.show()
    app.exec_()

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use a QStackedWidget

import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

ui_folder = os.path.join("frontend", "ui")
about_company_ui, _ = uic.loadUiType(os.path.join(ui_folder, "about_company.ui"))
intern_placement_ui, _ = uic.loadUiType(os.path.join(ui_folder, "intern_placement.ui"))


class InternPlacement(QtWidgets.QMainWindow, intern_placement_ui):
    def __init__(self, parent=None):
        super(InternPlacement, self).__init__(parent)
        self.setupUi(self)


class AboutCompany(QtWidgets.QMainWindow, about_company_ui):
    def __init__(self, parent=None):
        super(AboutCompany, self).__init__(parent)
        self.setupUi(self)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    intern_window = InternPlacement()
    about_window = AboutCompany()
    w = QtWidgets.QStackedWidget()
    w.addWidget(intern_window)
    w.addWidget(about_window)
    intern_window.intern_pushButton.clicked.connect(lambda: w.setCurrentIndex(1))
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...