我正在尝试让按钮在我用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()
发布于 2016-01-13 04:47:34
你的目标是什么?您不能只创建两个布局,将它们添加到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中是新的,所以请检查命名约定这里 ;)。
发布于 2016-01-13 07:43:37
多亏了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()
https://stackoverflow.com/questions/34754111
复制相似问题