【问题标题】:Switching Frames in Pyqt [duplicate]在 Pyqt 中切换框架 [重复]
【发布时间】:2019-07-11 12:28:26
【问题描述】:

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

在 tkinter 中,我创建了一个用于切换帧的 Controller 类,但我对 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

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

【问题讨论】:

  • 您是否研究过可堆叠小部件?我认为这听起来像是您想要实现的目标。

标签: python python-3.x pyqt pyqt5


【解决方案1】:

好吧,在 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_()

【讨论】:

  • 错误:NameError: name 'frame' is not defined
  • 哎呀。将 clear_frames 方法中的 frame.children 替换为 container.children。它应该可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多