我是python的新手。我想在一个主窗口上创建具有多个ui的应用程序。
例如在MainWindow
第一个ui:显示公司信息和一个按钮(下一个)。当用户单击“下一步”按钮时,转到第二个UI。
第二个ui:显示一些列表和两个按钮(下一个和后一个)。当用户单击“下一步”时,转到第三个UI。当用户点击“返回”按钮时,返回第一个ui(关于公司信息)。
第三个ui:显示一些信息和两个按钮(下一个和后一个),当用户点击“下一步”时,转到第四个ui。当用户单击“返回”按钮时,返回第二个ui(列出ui)。
我尝试如下。但我不能。我该怎么办?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class OtherWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
layout = QHBoxLayout()
lineEdit = QLineEdit()
lineEdit.setText("Just to fill up the dialog")
layout.addWidget(lineEdit)
button = QPushButton()
layout.addWidget(button)
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win2")
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = MainWindow()
self.myOtherWindow.show()
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
layout = QHBoxLayout()
button = QPushButton()
layout.addWidget(button)
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win1")
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 200, 200)
mainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
我明白了。基于this。我修改如下。
import sys
from PyQt4 import QtGui,QtCore
class LayoutTest(QtGui.QWidget):
def __init__(self):
super(LayoutTest, self).__init__()
self.first_box = QtGui.QVBoxLayout()
self.second_box = QtGui.QVBoxLayout()
self.third_box = QtGui.QVBoxLayout()
self.fourth_box = QtGui.QVBoxLayout()
self.zvbox = QtGui.QVBoxLayout()
vbox = QtGui.QVBoxLayout()
vbox.addLayout(self.first_box)
vbox.addLayout(self.second_box)
vbox.addLayout(self.third_box)
vbox.addLayout(self.fourth_box)
self.setLayout(vbox)
self.first_view()
self.setGeometry(300, 200, 400, 300)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
def first_view(self):
self.next1 = QtGui.QPushButton("NEXT 2")
self.first_box.addWidget(self.next1)
self.connect(self.next1, QtCore.SIGNAL("clicked()"), self.first_second_view)
def first_second_view(self):
self.remove_first_view()
self.second_view()
def second_view(self):
self.next3 = QtGui.QPushButton("NEXT 3")
self.back1 = QtGui.QPushButton("BACK 1")
self.second_box.addWidget(self.next3)
self.second_box.addWidget(self.back1)
self.connect(self.next3, QtCore.SIGNAL("clicked()"), self.second_third_view)
self.connect(self.back1, QtCore.SIGNAL("clicked()"), self.second_first_view)
def second_first_view(self):
self.remove_second_view()
self.first_view()
def second_third_view(self):
self.remove_second_view()
self.third_view()
def third_view(self):
self.next4 = QtGui.QPushButton("NEXT 4")
self.back2 = QtGui.QPushButton("BACK 2")
self.third_box.addWidget(self.next4)
self.third_box.addWidget(self.back2)
self.connect(self.next4, QtCore.SIGNAL("clicked()"), self.third_fourth_view)
self.connect(self.back2, QtCore.SIGNAL("clicked()"), self.third_second_view)
def third_second_view(self):
self.remove_third_view()
self.second_view()
def third_fourth_view(self):
self.remove_third_view()
self.back3 = QtGui.QPushButton("BACK 3")
self.fourth_box.addWidget(self.back3)
self.connect(self.back3, QtCore.SIGNAL("clicked()"), self.fourth_third_view)
def fourth_third_view(self):
self.remove_fourth_view()
self.third_view()
def remove_first_view(self):
for cnt in reversed(range(self.first_box.count())):
# takeAt does both the jobs of itemAt and removeWidget
# namely it removes an item and returns it
widget = self.first_box.takeAt(cnt).widget()
if widget is not None:
# widget will be None if the item is a layout
widget.deleteLater()
def remove_second_view(self):
for cnt in reversed(range(self.second_box.count())):
# takeAt does both the jobs of itemAt and removeWidget
# namely it removes an item and returns it
widget = self.second_box.takeAt(cnt).widget()
if widget is not None:
# widget will be None if the item is a layout
widget.deleteLater()
def remove_third_view(self):
for cnt in reversed(range(self.third_box.count())):
# takeAt does both the jobs of itemAt and removeWidget
# namely it removes an item and returns it
widget = self.third_box.takeAt(cnt).widget()
if widget is not None:
# widget will be None if the item is a layout
widget.deleteLater()
def remove_fourth_view(self):
for cnt in reversed(range(self.fourth_box.count())):
# takeAt does both the jobs of itemAt and removeWidget
# namely it removes an item and returns it
widget = self.fourth_box.takeAt(cnt).widget()
if widget is not None:
# widget will be None if the item is a layout
widget.deleteLater()
def checkItems(self):
QtGui.QMessageBox.information(self, 'Count',"You have %s Items in Layout" % self.dvbox.count(), QtGui.QMessageBox.Ok)
def run():
app = QtGui.QApplication(sys.argv)
ex = LayoutTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
run()
答案 1 :(得分:0)
这是使用QWizard
和QWizardPage
类的示例:
#!/usr/bin/env python
# -*- coding: utf-8 *-
import sys
import os
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
class MyWizard(QtGui.QWizard):
def __init__(self, parent = None):
QtGui.QWizard.__init__(self, parent)
self.result = 0
self.company_name = ""
self.company_listing = ""
self.info1 = ""
self.info2 = ""
self.info3 = ""
self.setWindowTitle("My wizard")
self.addPage(company_info(self))
self.addPage(company_listing(self))
self.addPage(company_other_infos(self))
self.finished.connect(self.end)
pass
def end(self, result):
self.result = result
if self.result:
self.company_name = str(self.field("company_name").toString())
self.company_listing = str(self.field("company_listing").toString())
self.info1 = str(self.field("info1").toString())
self.info2 = str(self.field("info2").toString())
self.info3 = str(self.field("info3").toString())
print self.company_name
print self.company_listing
print self.info1
print self.info2
print self.info3
class company_info(QtGui.QWizardPage):
def __init__(self, parent):
QtGui.QWizardPage.__init__(self, parent)
self.setTitle("Step 1/3")
self.setSubTitle("Company Name")
# Name
self.lineEdit = QtGui.QLineEdit("The world company")
# Fields
self.registerField("company_name", self.lineEdit)
# Page layout
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.lineEdit)
layout.addStretch(1)
self.setLayout(layout)
def isComplete(self):
if self.lineEdit.text() == "The world company":
self.completeChanged.emit()
return True
return False
class company_listing(QtGui.QWizardPage):
def __init__(self, parent):
QtGui.QWizardPage.__init__(self, parent)
self.setTitle("Step 2/3")
self.setSubTitle("Some listing")
ipsum = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris.
Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.
#Praesent et diam eget libero egestas mattis sit amet vitae augue.
Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem.
Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis.
Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet.
Ut convallis libero in urna ultrices accumsan. Donec sed odio eros.
Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est."""
# Some listing in a table widget
textEdit = QtGui.QTextEdit()
textEdit.setPlainText(ipsum)
# Fields
self.registerField("company_listing", textEdit, "plainText")
# Page layout
layout = QtGui.QVBoxLayout(self)
layout.addWidget(textEdit)
layout.addStretch(1)
self.setLayout(layout)
pass
def initializePage(self):
pass
def cleanupPage(self):
pass
class company_other_infos(QtGui.QWizardPage):
def __init__(self, parent):
QtGui.QWizardPage.__init__(self, parent)
self.setTitle("Step 3/3")
self.setSubTitle("Other infos")
# Other infos
lineEdit_info1 = QtGui.QLineEdit("info1")
lineEdit_info2 = QtGui.QLineEdit("info2")
lineEdit_info3 = QtGui.QLineEdit("info3")
# Fields
self.registerField("info1", lineEdit_info1)
self.registerField("info2", lineEdit_info2)
self.registerField("info3", lineEdit_info3)
# Page layout
layout = QtGui.QVBoxLayout(self)
layout.addWidget(lineEdit_info1)
layout.addWidget(lineEdit_info2)
layout.addWidget(lineEdit_info3)
layout.addStretch(1)
self.setLayout(layout)
def initializePage(self):
pass
def cleanupPage(self):
pass
myWizard = MyWizard()
myWizard.show()
sys.exit(app.exec_())