gpt4 book ai didi

python - 如何在PyQt5中制作多页面应用程序?

转载 作者:行者123 更新时间:2023-11-30 21:55:23 25 4
gpt4 key购买 nike

我想在 PyQt 中制作一个简单的 GUI 应用程序。单击按钮时,它应该带您进入下一页。这就像您在安装程序时所知道的这些安装对话框。

我尝试在与主窗口完全相同的位置打开一个新窗口,但感觉不对。

Soo,这是我的代码:

class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "MainWindow"
# Here are some height & width variables
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.UiComponents()
def UiComponents(self):
self.searchButton = QPushButton("", self)
# alot of UiComponents go here
self.searchButton.clicked.connect(self.make_handleButton("searchButton"))
def make_handleButton(self, button):
def handleButton():
if button == "searchButton":
### here it should go to SearchWindow ###
#elif button == "importButton":
# self.importWindow()
return handleButton

class SearchWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Search for something"
# Here are some height & width variables
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.UiComponents()
self.hide()
def goToMain(self):
### here it should go back to the MainWindow ###
def UiComponents(self):
self.backButton = QPushButton("BackButton", self)
self.backButton.setGeometry(QRect(5, 5, self.backButtonWidth, self.backButtonHeight))
self.backButton.clicked.connect(self.goToMain)

最佳答案

你必须使用QStackWidget,下面的部分有一个例子:

from PyQt5 import QtCore, QtGui, QtWidgets


class PageWindow(QtWidgets.QMainWindow):
gotoSignal = QtCore.pyqtSignal(str)

def goto(self, name):
self.gotoSignal.emit(name)


class MainWindow(PageWindow):
def __init__(self):
super().__init__()
self.initUI()
self.setWindowTitle("MainWindow")

def initUI(self):
self.UiComponents()

def UiComponents(self):
self.searchButton = QtWidgets.QPushButton("", self)
self.searchButton.clicked.connect(
self.make_handleButton("searchButton")
)

def make_handleButton(self, button):
def handleButton():
if button == "searchButton":
self.goto("search")
return handleButton


class SearchWindow(PageWindow):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle("Search for something")
self.UiComponents()

def goToMain(self):
self.goto("main")

def UiComponents(self):
self.backButton = QtWidgets.QPushButton("BackButton", self)
self.backButton.setGeometry(QtCore.QRect(5, 5, 100, 20))
self.backButton.clicked.connect(self.goToMain)


class Window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)

self.stacked_widget = QtWidgets.QStackedWidget()
self.setCentralWidget(self.stacked_widget)

self.m_pages = {}

self.register(MainWindow(), "main")
self.register(SearchWindow(), "search")

self.goto("main")

def register(self, widget, name):
self.m_pages[name] = widget
self.stacked_widget.addWidget(widget)
if isinstance(widget, PageWindow):
widget.gotoSignal.connect(self.goto)

@QtCore.pyqtSlot(str)
def goto(self, name):
if name in self.m_pages:
widget = self.m_pages[name]
self.stacked_widget.setCurrentWidget(widget)
self.setWindowTitle(widget.windowTitle())


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

关于python - 如何在PyQt5中制作多页面应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com