【发布时间】:2020-07-09 07:15:59
【问题描述】:
首先感谢您抽出宝贵时间,抱歉问题太长了,真正的问题只是第一部分。 我无法理解如何实现“页面”更改,例如 Web 应用程序。 我想使用代表小部件或主窗口的 .ui 文件来做到这一点。 基本上,我希望有两个类,每个类都映射有一个实现视图的 .ui 文件,并在单击按钮时在两个视图之间切换。
我想要的是这样的:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# then load the home page file home.ui
class HomePage:
def method_0(self):
# method to load and show the home.ui file, at startup
def method_1(self):
# method to load and show the home.ui file, when a button in Second Page is pressed
class SecondPage
def method_1(self):
# method to load and show the second.ui file when a button in HomePage is pressed
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
经过大量研究,我编写了下面的代码,几乎意识到,我说几乎是因为我必须为所有人使用同一个类。相反,我想要的是为每个 .ui 文件创建一个 .py 文件以保持分离。
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QStackedWidget
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(800, 620)
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.home_page()
def home_page(self):
widget = uic.loadUi('home.ui')
self.central_widget.addWidget(widget)
self.central_widget.setCurrentWidget(widget)
widget.change_btn.clicked.connect(self.second_page)
def second_page(self):
widget = uic.loadUi('second.ui')
self.central_widget.addWidget(widget)
self.central_widget.setCurrentWidget(widget)
widget.change_btn.clicked.connect(self.home_page)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
这是两个 .ui 文件的代码,只需将文件名更改为 home.ui 和 second.ui,并更改 btn 文本以查看页面更改。它们是简单的小部件,每个都有一个按钮
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>620</height>
</rect>
</property>
<property name="windowTitle">
<string>Application</string>
</property>
<widget class="QWidget" name="centralwidget">
<property name="title" stdset="0">
<string>GroupBox</string>
</property>
<widget class="QPushButton" name="change_btn">
<property name="geometry">
<rect>
<x>285</x>
<y>170</y>
<width>230</width>
<height>40</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Change page</string>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
我不确定我是否完全理解你。您想要一个单个主窗口,并显示应该加载自己的 .ui 文件的不同“页面”,并在单独的文件中加载这些“页面”的代码?
-
是的,完全正确。它不必是“单个主窗口”,如果它工作一切都很好,但我不希望用户看到窗口关闭并重新打开新页面(当我尝试使用时发生在我身上两个主窗口)
-
例如一个带有 {HomePage} 类的 'Home.py' 文件,它加载 'home.ui 文件并具有按钮功能的方法,最重要的是转到另一个类的方法另一个 .ui 代码。我现在拥有的代码没有针对不同页面的不同类。
标签: python user-interface pyqt pyqt5