在 Pyqt 中切换帧 [英] Switching Frames in Pyqt

查看:43
本文介绍了在 Pyqt 中切换帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 tkinter 构建了一个应用程序来在单个平台上执行不同的分析,但现在我正在尝试将其转换为 PyQT 以改进演示文稿的可视化.我的应用由两个不同的框架组成,可通过主框架的按钮访问这些框架.

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.

在 tkinter 中,我创建了一个用于切换帧的 Controller 类,但我真的是 PyQt 的新手,我无法实现它或任何合理的错误代码.我简化了下面的代码.

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

我想在开始时打开主窗口,然后使用主窗口上的按钮打开函数的框架.使用功能框架内的后退按钮,我想返回上一帧.而且我还想使用 Function1Button 从功能 1 到达功能 2 的框架,反之亦然.

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.

推荐答案

好吧,在 PyQt5(或 PySide2)中,您需要将 QFrame 设置为容器".在里面你会放其他 QFrames.

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_()

这篇关于在 Pyqt 中切换帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆