[英]PyQt4 QStackedLayout Switching Layout Error
I'm trying to get the buttons to work on a user interface I'm making with PyQt4. 我正在尝试让按钮在我使用PyQt4制作的用户界面上工作。 The windows change but the button only works once before I get the error NameError: name 'UI2Window' is not defined . 窗口改变了,但是在我收到错误NameError之前,该按钮仅工作一次:名称'UI2Window'未定义 。 I don't understand why I get this error as the code works fine the first time. 我不明白为什么会在代码第一次正常运行时收到此错误。
Help fixing this issue / finding a better way to switch layouts is appreciated. 感谢您解决此问题/找到更好的切换布局方法。
A simplified version on my code is below. 下面是我代码的简化版本。
Code from UI1.py: 来自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()
Code from UI".py: 来自UI的代码。
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()
What is your goal? 你的目标是什么? Can't you just create your two layouts add them to the QStackedLayout and then switch between index 0 and 1? 您不能只创建两个布局将它们添加到QStackedLayout中,然后在索引0和1之间切换吗?
Something llike that to keep your code: 可以保留您的代码的东西:
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()
I assume you are new in python so please check naming convention here ;). 我假设您是python的新手,所以请在此处检查命名约定;)。
I got it working thanks to Controlix's help. 多亏了Controlix的帮助,我才能正常工作。 The code is below if anyone else needs it. 如果其他人需要,则下面的代码。
Window Controller Code: 窗口控制器代码:
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()
Code For Windows: 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.