我已经构建了一个应用程序,可以使用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从Function 1到达Function 2的帧,反之亦然。
答案 0 :(得分:0)
好吧,在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_()