本实例环境ubuntu16+python3.5+PySide2==5.12
学习pyqt5两个月,一直不明白类初始化函数__init__(self, parent)中parent的作用,如果本窗口需要继承其他窗口, 这里的parent就应该填parent=继承的窗口类.
创建一个主窗口
'''该文件命名为mainWin.py'''
from PySide2.QtWidgets import QWidget, QCheckBox, QApplication, QPushButton, QLabel, QVBoxLayout, QFormLayout
import subFirst
import sys
class Example(QWidget):
def __init__(self):
QWidget.__init__(self)
self.initUI()
self.setWindowTitle('主窗口')
def initUI(self):
self.cb = QCheckBox('复选框', self) #添加一个复选框
self.cb.stateChanged.connect(self.changecb) #关联复选框槽函数
self.bt = QPushButton('确定', self) #添加一个按钮
self.bt.clicked.connect(self.go) #关联按钮槽函数
self.label = QLabel() #添加一个标签
self.formlayout = QFormLayout()
self.formlayout.addRow(self.cb, self.label)
self.vbox = QVBoxLayout()
self.vbox.addLayout(self.formlayout)
self.vbox.addWidget(self.bt)
self.setLayout(self.vbox)
self.resize(400, 300)
self.show()
def go(self): #按钮槽函数
if self.cb.isChecked():
subFirstWindow = subFirst.firstSub(self) #打开第一个子窗口
subFirstWindow.show()
else:
pass
def changecb(self): #复选框槽函数
if self.cb.isChecked():
self.label.setText('单选框已被勾选,点击确定跳转到子窗口')
else:
self.label.setText(' ')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
创建第一个子窗口
'''该文件命名为subFirst.py'''
import sys
import subSecond, mainWin
from PySide2.QtWidgets import QDialog, QVBoxLayout, QPushButton, QTextEdit, QLabel, QFormLayout, QApplication
class firstSub(QDialog):
def __init__(self, parent=None): #parent参数默认为None
QDialog.__init__(self, parent) #传入parent参数,想要继承那个窗口,parent就填谁
self.initUi()
self.setWindowTitle('窗口1')
def initUi(self):
btn = QPushButton('跳到第二个子窗口')
btn.clicked.connect(self.btnfunc)
label = QLabel()
textEdit = QTextEdit()
formlayout = QFormLayout()
vbox = QVBoxLayout()
label.setText('表格')
formlayout.addRow(label, textEdit)
vbox.addLayout(formlayout)
vbox.addWidget(btn)
self.setLayout(vbox)
self.show()
def btnfunc(self):
sub2 = subSecond.secondSub(self) #跳到第二个子窗口, self指向本类,所以secondSub继承本窗口,即继承firstSub
sub2.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
sub1 = firstSub()
sys.exit(app.exec_())
创建第二个子窗口
'''该文件命名为subSecond.py'''
from PySide2.QtWidgets import QLabel, QHBoxLayout, QApplication, QDialog, QPushButton
import sys
import subThird
class secondSub(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.initUi()
def initUi(self):
self.setWindowTitle('窗口2')
btn = QPushButton('跳到第三个子窗口')
btn.clicked.connect(self.btnfunc)
hbox = QHBoxLayout()
hbox.addWidget(btn)
self.setLayout(hbox)
self.resize(200, 200)
self.show()
def btnfunc(self):
sub3 = subThird.thirdSub(self)#跳到第三个子窗口, self指向本类,所以thirdSub继承本窗口,即继承secondSub
sub3.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
sub2 = secondSub()
sys.exit(app.exec_())
创建第三个子窗口
'''该文件命名为subThird.py'''
from PySide2.QtWidgets import QDialog, QLineEdit, QVBoxLayout, QApplication
import sys
class thirdSub(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.initUi()
def initUi(self):
self.setWindowTitle('窗口3')
lineEdit = QLineEdit()
lineEdit.setText('第三个子窗口')
vbox = QVBoxLayout()
vbox.addWidget(lineEdit)
self.setLayout(vbox)
self.resize(200, 100)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
sub3 = thirdSub()
sys.exit(app.exec_())