Pyq中的交换帧

2024-04-24 22:04:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用tkinter构建了一个应用程序来在一个平台上执行不同的分析,但是现在我正试图将它转换为PyQT,以提高演示的可视化效果。我的应用程序由两个不同的框架组成,它们可以通过我的主框架的按钮访问。在

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

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

我想在开始时打开主窗口,然后用主窗口上的按钮打开函数的框架。 有了后退按钮,我想返回上一帧。 我还想用function1按钮从函数1到达函数2的帧,反之亦然。在


Tags: toselfgodefconnectwithbuttonthis
1条回答
网友
1楼 · 发布于 2024-04-24 22:04:04

在PyQt5(或PySide2)中,需要将QFrame设置为“容器”。在里面你会放其他的Qframe。在

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

相关问题 更多 >