多个文件中的 PyQt 小部件

发布于 2024-12-08 11:47:36 字数 429 浏览 0 评论 0原文

我想通过编写一个简单的游戏来学习 PyQt。第一个小部件将具有“新游戏”、“退出”等按钮。我无法理解如何从该菜单小部件转换到新的菜单小部件。

例如,如果我单击“新游戏”,如何出现一个新的小部件来替换旧的小部件并询问用户名?我现在处理它的方式就像

Form = QtGui.QWidget()
ui = uiMainMenu()
ui.setupUi(Form)
Form.show()

一旦按下 newGameButton ,它就会进入一个子例程...

Form2 = QtGui.QWidget()
ui2 = uiNewGame()
ui2.setupUi(Form2)
Form2.show()

我并不要求所有代码,只是解释我应该如何处理这个问题,因为代码以上不是深蹲。
谢谢!

i'd like to learn PyQt by writing a simple game. the first widget would have buttons like "New game", "Quit", etc. i am having trouble understanding how to transition from that menu widget to a new one.

for instance, if i were to click New Game, how do i have a new widget appear that replaces the old one and asks for the user's name? the way i am approaching it now is something like

Form = QtGui.QWidget()
ui = uiMainMenu()
ui.setupUi(Form)
Form.show()

then once newGameButton is pressed it would go to a subroutine...

Form2 = QtGui.QWidget()
ui2 = uiNewGame()
ui2.setupUi(Form2)
Form2.show()

i'm not asking for all the code, just an explanation as to how i should be approaching the problem, because the code above ain't doing squat.
thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2024-12-15 11:47:36

如果你想在表单之间切换,那么你可以使用 QStackedWidget。
您可以在下面找到一个工作示例代码:

import sys
from functools import partial
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Form1(QWidget):
    showForm2Signal = pyqtSignal()

    def __init__(self, parent=None):
        super(Form1, self).__init__(parent)
        self.newGameButton = QPushButton("New Game", self)
        self.quitButton = QPushButton("Quit", self)
        layout = QVBoxLayout(self)
        layout.addWidget(QLabel("<html>My Game<br>Start Page</html>"))
        layout.addWidget(self.newGameButton)
        layout.addWidget(self.quitButton)
        self.newGameButton.clicked.connect(self.showForm2Signal.emit)
        self.quitButton.clicked.connect(qApp.quit)


class Form2(QWidget):
    showForm1Signal = pyqtSignal()

    def __init__(self, parent=None):
        super(Form2, self).__init__(parent)
        self.backButton = QPushButton("Back", self)
        layout = QVBoxLayout(self)
        layout.addWidget(QLabel("New Game Started!"))
        layout.addWidget(self.backButton)
        self.backButton.clicked.connect(self.showForm1Signal.emit)


class MainWidget(QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.stack = QStackedWidget()
        layout = QVBoxLayout(self)
        layout.addWidget(self.stack)
        self.form1 = Form1(self)
        self.form2 = Form2(self)
        self.stack.addWidget(self.form1)
        self.stack.addWidget(self.form2)
        self.form1.showForm2Signal.connect(partial(self.stack.setCurrentWidget,
                                               self.form2))
        self.form2.showForm1Signal.connect(partial(self.stack.setCurrentWidget,
                                               self.form1))
        self.stack.setCurrentWidget(self.form1)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    w.show()
    app.exec_()
    sys.exit()

如果您只想向用户询问名称,那么您可以使用 QDialog 小部件。

if you want to switch between forms then you can use QStackedWidget.
Below you can find a working sample code:

import sys
from functools import partial
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Form1(QWidget):
    showForm2Signal = pyqtSignal()

    def __init__(self, parent=None):
        super(Form1, self).__init__(parent)
        self.newGameButton = QPushButton("New Game", self)
        self.quitButton = QPushButton("Quit", self)
        layout = QVBoxLayout(self)
        layout.addWidget(QLabel("<html>My Game<br>Start Page</html>"))
        layout.addWidget(self.newGameButton)
        layout.addWidget(self.quitButton)
        self.newGameButton.clicked.connect(self.showForm2Signal.emit)
        self.quitButton.clicked.connect(qApp.quit)


class Form2(QWidget):
    showForm1Signal = pyqtSignal()

    def __init__(self, parent=None):
        super(Form2, self).__init__(parent)
        self.backButton = QPushButton("Back", self)
        layout = QVBoxLayout(self)
        layout.addWidget(QLabel("New Game Started!"))
        layout.addWidget(self.backButton)
        self.backButton.clicked.connect(self.showForm1Signal.emit)


class MainWidget(QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.stack = QStackedWidget()
        layout = QVBoxLayout(self)
        layout.addWidget(self.stack)
        self.form1 = Form1(self)
        self.form2 = Form2(self)
        self.stack.addWidget(self.form1)
        self.stack.addWidget(self.form2)
        self.form1.showForm2Signal.connect(partial(self.stack.setCurrentWidget,
                                               self.form2))
        self.form2.showForm1Signal.connect(partial(self.stack.setCurrentWidget,
                                               self.form1))
        self.stack.setCurrentWidget(self.form1)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    w.show()
    app.exec_()
    sys.exit()

If you only want to ask the name to the user then you can use a QDialog widget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文