PyQT4 - 替换主窗口上的小部件

发布于 2024-12-18 10:00:20 字数 3242 浏览 2 评论 0原文

我是 PyQT4 的新手,我遇到了以下问题。

某些应用程序必须在一个屏幕上收集用户数据 并在单击按钮时显示下一个屏幕。

ma​​in.py

app = QtGui.QApplication(sys.argv)
#here I create main window with inherited class
w = SAN_MainWindow()
#and apply some basic geometry
w.SetUI()
#here first screen rendered and button event applyed
w.ApplyWidget( SAN_Intro() )
w.show()
sys.exit(app.exec_())

第一个小部件的渲染正确发生。

SAN_MainWindow.py:

class SAN_MainWindow(QtGui.QMainWindow):
    def __init__(self ):
        super(SAN_MainWindow, self).__init__()

    def SetUI(self):
        self.setObjectName(_fromUtf8("MainWindow"))
        self.resize(789, 602)
        self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
        self.central_widget = QtGui.QWidget(self)   
        self.central_widget.setObjectName(_fromUtf8("central_widget"))
        self.setCentralWidget(self.central_widget)

    def ApplyWidget( self, widget ):
        self.active_widget = widget
        self.active_widget.Setup(self.central_widget)
        self.active_widget.SetControls( self )

    def RemoveActiveWidget( self ):
        self.active_widget.widget().setParent(None)

    def ShowInstruction( self ):
        self.RemoveActiveWidget()
        self.ApplyWidget( SAN_Instruction() )
        #self.centralWidget().update() <- Problem

SAN_Intro.py:

class SAN_Intro(object):
    def __init__(self):
        super(SAN_Intro, self).__init__()

    def Setup(self, widget):
        self.gridLayoutWidget = QtGui.QWidget(widget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(80, 30, 638, 451))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(-1, 16, 16, -1)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        #a lot of form inputs instructions....
    def SetControls( self, main_window ):
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), main_window.ShowInstruction)

    def widget(self):
        return self.gridLayoutWidget

SAN_Instruction 与 SAN_Intro 几乎相同,但在设置中使用不同的命令:

class SAN_Instruction(object):

    def __init__(self):
        super(SAN_Instruction, self).__init__()

    def Setup(self, widget):
        self.verticalLayoutWidget = QtGui.QWidget(widget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(40, 20, 591, 521))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        #a lot of form inputs instructions.... 

    def SetControls( self, main_window ):
        pass

    def widget(self):
        return self.verticalLayoutWidget

这里是问题: 我必须如何显示第二个(使用 SAN_Instruction 动态生成)小部件? 我尝试更新/显示/重新绘制主窗口/centralwidget/parent。 但我点击按钮时得到的只是 self.RemoveActiveWidget()。

也许我的概念不正确?

提前感谢

I`m new in PyQT4 and I've faced the problem below.

Some application must collect users' data on one screen
and show next screen on button click.

main.py

app = QtGui.QApplication(sys.argv)
#here I create main window with inherited class
w = SAN_MainWindow()
#and apply some basic geometry
w.SetUI()
#here first screen rendered and button event applyed
w.ApplyWidget( SAN_Intro() )
w.show()
sys.exit(app.exec_())

Rendering of first widget occurs correctly.

SAN_MainWindow.py:

class SAN_MainWindow(QtGui.QMainWindow):
    def __init__(self ):
        super(SAN_MainWindow, self).__init__()

    def SetUI(self):
        self.setObjectName(_fromUtf8("MainWindow"))
        self.resize(789, 602)
        self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
        self.central_widget = QtGui.QWidget(self)   
        self.central_widget.setObjectName(_fromUtf8("central_widget"))
        self.setCentralWidget(self.central_widget)

    def ApplyWidget( self, widget ):
        self.active_widget = widget
        self.active_widget.Setup(self.central_widget)
        self.active_widget.SetControls( self )

    def RemoveActiveWidget( self ):
        self.active_widget.widget().setParent(None)

    def ShowInstruction( self ):
        self.RemoveActiveWidget()
        self.ApplyWidget( SAN_Instruction() )
        #self.centralWidget().update() <- Problem

SAN_Intro.py:

class SAN_Intro(object):
    def __init__(self):
        super(SAN_Intro, self).__init__()

    def Setup(self, widget):
        self.gridLayoutWidget = QtGui.QWidget(widget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(80, 30, 638, 451))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(-1, 16, 16, -1)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        #a lot of form inputs instructions....
    def SetControls( self, main_window ):
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), main_window.ShowInstruction)

    def widget(self):
        return self.gridLayoutWidget

SAN_Instruction is almost the same as SAN_Intro, but with different commands in Setup:

class SAN_Instruction(object):

    def __init__(self):
        super(SAN_Instruction, self).__init__()

    def Setup(self, widget):
        self.verticalLayoutWidget = QtGui.QWidget(widget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(40, 20, 591, 521))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        #a lot of form inputs instructions.... 

    def SetControls( self, main_window ):
        pass

    def widget(self):
        return self.verticalLayoutWidget

AND HERE IS QUESTION:
How must I show the second (generated on fly with SAN_Instruction) widget?
I tried to update/show/repaint mainwindow/centralwidget/parent.
But all I've got on button click is self.RemoveActiveWidget().

Maybe my concept is incorrect?

Thanx in advance

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

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

发布评论

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

评论(2

逐鹿 2024-12-25 10:00:20

好吧,您的代码不完整,但我建议您利用 hide() 方法。
您可以在按钮函数中定义它,以便隐藏所有您不想显示的小部件,并在同一函数上调用您想要在其位置出现的小部件。我过去常常这样做,因为它使我的项目更有组织性并且易于跟踪和调试。此外,如果需要,这还允许您使用 show() 方法使该小部件再次可见。

Well, your code isn't complete but i would advise you to take advantage of the hide() method.
You can define this in your button function, so that all the widgets you do not want to appear get hidden, and on the same function call the widgets you want to appear on their place. I use to do that way because it makes my projects more organized and easy to track and debug. Furthermore, this also allows you to make that widgets visible again if you need, with the show() method.

最终幸福 2024-12-25 10:00:20

您可以使用“QtGui.QStackedWidget()”。您将窗口拆分为许多框架,将每个框架添加到 QStackedWidget() 并使用函数 setCurrentWidget() 来选择要显示的框架(在框架之间切换)。

self.central_widget.addWidget(self.mainFrame)
self.central_widget.setCurrentWidget(self.mainFrame)

You can use a "QtGui.QStackedWidget()". You Split your Window to many Frames, add each frame to the QStackedWidget() and use function setCurrentWidget() to choose which frame to display (switch between frames).

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