PyQt4 QStackedLayout切换布局错误

时间:2016-01-12 21:28:59

标签: python-3.x pyqt4

我正在尝试让按钮在我正在使用PyQt4制作的用户界面上工作。窗口发生变化但按钮仅在我收到错误之前工作一次 NameError:名称'UI2Window'未定义。我不明白为什么我得到这个错误,因为代码第一次工作正常。

帮助解决此问题/寻找切换布局的更好方法,我们表示赞赏。

我的代码的简化版本如下。

来自UI1.py的代码:

import sys
from PyQt4.QtCore import*
from PyQt4.QtGui import *
from UI2 import *

class UI1Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.Widget = self.CreateMainLayout()
        self.StackedLayout = QStackedLayout()
        self.StackedLayout.addWidget(self.Widget)
        self.MainWidget = QWidget()
        self.MainWidget.setLayout(self.StackedLayout)
        self.setCentralWidget(self.MainWidget)

    def OnClicked(self):
        Window = UI2Window()
        self.StackedLayout.addWidget(Window)
        self.StackedLayout.setCurrentIndex(1)

    def CreateMainLayout(self):
        Widget = QWidget()
        self.VLayout = QVBoxLayout()
        self.Button = QPushButton("UI2")
        self.Button.clicked.connect(self.OnClicked)
        self.VLayout.addWidget(self.Button)     
        Widget.setLayout(self.VLayout)
        return Widget

def main():
    App = QApplication(sys.argv)
    ProgramWindow = UI1Window()
    ProgramWindow.show()
    App.exec_()

if __name__ == "__main__":
    main()

来自UI的代码“.py:

import sys
from PyQt4.QtCore import*
from PyQt4.QtGui import *
from UI1 import *

class UI2Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.Widget = self.CreateMainLayout()
        self.StackedLayout = QStackedLayout()
        self.StackedLayout.addWidget(self.Widget)
        self.MainWidget = QWidget()
        self.MainWidget.setLayout(self.StackedLayout)
        self.setCentralWidget(self.MainWidget)

    def OnClicked(self):
        Window = UI1Window()
        self.StackedLayout.addWidget(Window)
        self.StackedLayout.setCurrentIndex(1)

    def CreateMainLayout(self):
        Widget = QWidget()
        self.VLayout = QVBoxLayout()
        self.Button = QPushButton("UI1")
        self.Button.clicked.connect(self.OnClicked)
        self.VLayout.addWidget(self.Button)     
        Widget.setLayout(self.VLayout)
        return Widget

def main():
    App = QApplication(sys.argv)
    ProgramWindow = UI2Window()
    ProgramWindow.show()
    App.exec_()

if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:2)

你的目标是什么?难道你不能只创建两个布局将它们添加到QStackedLayout然后在索引0和1之间切换吗?

保留代码的东西:

import sys
from PyQt4.QtCore import*
from PyQt4.QtGui import *
from UI2 import *

class UI1Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.StackedLayout = QStackedLayout()
        self.StackedLayout.addWidget(self.CreateMainLayout())
        self.StackedLayout.addWidget(self.CreateMainLayout2())
        self.MainWidget = QWidget()
        self.MainWidget.setLayout(self.StackedLayout)
        self.setCentralWidget(self.MainWidget)

    def OnClicked(self):
        self.StackedLayout.setCurrentIndex(1 if self.StackedLayout.currentIndex() == 0 else 0)

    def CreateMainLayout(self):
        Widget = QWidget()
        self.VLayout = QVBoxLayout()
        self.Button = QPushButton("UI2")
        self.Button.clicked.connect(self.OnClicked)
        self.VLayout.addWidget(self.Button)
        Widget.setLayout(self.VLayout)
        return Widget

    def CreateMainLayout2(self):
        Widget = QWidget()
        self.VLayout = QVBoxLayout()
        self.Button = QPushButton("UI1")
        self.Button.clicked.connect(self.OnClicked)
        self.VLayout.addWidget(self.Button)
        Widget.setLayout(self.VLayout)
        return Widget

def main():
    App = QApplication(sys.argv)
    ProgramWindow = UI1Window()
    ProgramWindow.show()
    App.exec_()

if __name__ == "__main__":
    main()

我假设你是python中的新手,所以请检查命名约定here;)。

答案 1 :(得分:1)

感谢Controlix的帮助,我得到了它。如果其他人需要,代码如下。

窗口控制器代码:

import sys
from PyQt4.QtCore import*
from PyQt4.QtGui import *
from UI1 import *
from UI2 import *

class Controller(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ImportWindows()
        self.ConnectButtons()
        self.StackedLayout = QStackedLayout()
        self.StackedLayout.addWidget(self.Window1)
        self.StackedLayout.addWidget(self.Window2)
        self.MainWidget = QWidget()
        self.MainWidget.setLayout(self.StackedLayout)
        self.setCentralWidget(self.MainWidget)

    def OnClicked1(self):
        self.StackedLayout.setCurrentIndex(1)

    def OnClicked2(self):
        self.StackedLayout.setCurrentIndex(0)

    def ImportWindows(self):
        self.Window1 = UI1Window()
        self.Window2 = UI2Window()

    def ConnectButtons(self):
        self.Window1.Button.clicked.connect(self.OnClicked1)
        self.Window2.Button.clicked.connect(self.OnClicked2)

def main():
    App = QApplication(sys.argv)
    ProgramWindow = Controller()
    ProgramWindow.show()
    App.exec_()

if __name__ == "__main__":
    main()

Windows代码:

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

class UI1Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.StackedLayout = QStackedLayout()
        self.StackedLayout.addWidget(self.CreateMainLayout())
        self.MainWidget = QWidget()
        self.MainWidget.setLayout(self.StackedLayout)
        self.setCentralWidget(self.MainWidget)

    def CreateMainLayout(self):
        Widget = QWidget()
        self.VLayout = QVBoxLayout()
        self.Button = QPushButton("UI2")
        self.VLayout.addWidget(self.Button)     
        Widget.setLayout(self.VLayout)
        return Widget

def main():
    App = QApplication(sys.argv)
    ProgramWindow = UI1Window()
    ProgramWindow.show()
    App.exec_()

if __name__ == "__main__":
    main()