Switching Frames in Pyqt [duplicate]

2019-08-22 22:41发布

问题:

This question already has an answer here:

  • How do I switch layouts in a window using PyQt?? (Without closing/opening windows) 1 answer

I've built an application to perform different analyses in a single platform with using tkinter but now I'm trying to convert it to PyQT to improve visualisation for a presentation. My app consists of two different frames that are reachable from the buttons of my main frame.

In tkinter I created a Controller class for switching frames but I'm really new on PyQt and I could not achieve it or any reasonable error code. I simplified my code below.

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MainWindow(object):
    def setupUi(self, MainFrame):
        MainFrame.setObjectName("MainFrame")
        MainFrame.resize(870, 230)
        MainFrame.setFrameShape(QFrame.Box)

        self.Function1Button = QPushButton(MainFrame)
        #I want to go Function 1's frame with this button
        self.Function1Button.clicked.connect(????)
        self.Function2Button = QPushButton(MainFrame)
        #I want to go Function 2's frame with this button
        self.Function2Button.clicked.connect(????)

class Function1(object):
    def setupUi(self, Frame):
        Frame.setObjectName("Frame")
        Frame.resize(870, 230)

        self.BackButton = QPushButton(MainFrame)
        # I want to go previous frame with this button
        self.BackButton.clicked.connect(????)
        self.Function2Button = QPushButton(MainFrame)
        #I want to go Function 2's frame with this button
        self.Function2Button.clicked.connect(????)
        self.ExecuteButton = QPushButton(MainFrame)
        self.ExecuteButton.clicked.connect(self.runfunc)

    def runfunc(self):
        # Computations

class Function2(object):
    def setupUi(self, Frame):
        Frame.setObjectName("Frame")
        Frame.resize(870, 230)

        self.BackButton = QPushButton(MainFrame)
        self.BackButton.clicked.connect(????)
        self.Function1Button = QPushButton(MainFrame)
        #I want to go Function 1's frame with this button
        self.Function1Button.clicked.connect(????)
        self.ExecuteButton = QPushButton(MainFrame)
        self.ExecuteButton.clicked.connect(self.runfunc)

    def runfunc(self):
        # Computations

I want to open my mainwindow on start, then with the buttons on my mainwindow, I want to open functions' frames. With the back buttons inside of the function frames I want to return previous frame. And also I want to reach Function 2's frame from Function 1 with Function1Button or vice versa.

回答1:

Well, in PyQt5 (or PySide2) you need to set a QFrame as a "container". Inside it you'll put the other QFrames.

import sys
from PySide2 import QtWidgets #here I'm using PySide2, but you can use PyQt5 as well


class Ui_frame_contained():
#class that implements the widgets for the child frame

    def setWidgets(self, frame_container):
        self.horizontalLayout = QtWidgets.QHBoxLayout(frame_container) 
        self.frame_contained = QtWidgets.QFrame(frame_container) 
        self.horizontalLayout2 = QtWidgets.QHBoxLayout(self.frame_contained)
        self.test_label = QtWidgets.QLabel('Test')
        self.horizontalLayout_2.addWidget(self.test_label)
        self.horizontalLayout.addWidget(self.frame_contained)



class child_frame(Ui_frame_contained):

     def setFrame(self, frame):
         super(child_frame, self).setWidgets(frame)
         self.frame_contained.show()


class Main(QtWidgets.QMainWindow, child_frame):

    def __init__(self):
        super(Main, self).__init__()
        self.setupUi(self)
        self.button.clicked.connect(self.open_frame)

    def setupUi(self, MainWindow):
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.boxLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.button = QtWidgets.QPushButton(self.centralwidget)
        self.frame_container = QtWidgets.QFrame(self.centralwidget)
        self.boxLayout.addWidget(self.frame_container)
        self.boxLayout.addWidget(self.button)
        MainWindow.setCentralWidget(self.centralwidget)

    def clear_frames(self,container):
        if len(frame.children()) > 0:
            for i in range(len(frame.children())):
                frame.children()[i].deleteLater()

    def open_frame(self):
        self.clear_frames(self.frame_container)
        #clear all the old frames before open a new one
        self.setFrame(self.frame_container)




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = Main()
    main_window.show()
    app.exec_()