窗体间传值网上有好多方法,比如新建文件,先将子类窗体的数据传到文件中,父窗体读取文件、 Signal&Slot机制进行传值 等等
在这里,我们就举个采用apply方法:Signal&Slot的例子
不必多说,三个文件搞定一切!
parent.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>339</width>
<height>201</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="BtnOpenC">
<property name="geometry">
<rect>
<x>250</x>
<y>150</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>子窗体</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>301</width>
<height>91</height>
</rect>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
child.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>339</width>
<height>182</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>100</x>
<y>50</y>
<width>221</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>50</y>
<width>54</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Data:</string>
</property>
</widget>
<widget class="QPushButton" name="pushButtonOK">
<property name="geometry">
<rect>
<x>150</x>
<y>140</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Ok</string>
</property>
</widget>
<widget class="QPushButton" name="pushButtonCancel">
<property name="geometry">
<rect>
<x>240</x>
<y>140</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Cancel</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
分别转换为py
parent.py:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'parent.ui'
#
# Created: Wed Mar 25 16:14:25 2015
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig) class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(339, 201)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.BtnOpenC = QtGui.QPushButton(self.centralwidget)
self.BtnOpenC.setGeometry(QtCore.QRect(250, 150, 75, 23))
self.BtnOpenC.setObjectName(_fromUtf8("BtnOpenC"))
self.textEdit = QtGui.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(20, 20, 301, 91))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.BtnOpenC.setText(_translate("MainWindow", "子窗体", None)) if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
child.py:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'child.ui'
#
# Created: Wed Mar 25 16:14:29 2015
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(339, 182)
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(100, 50, 221, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(20, 50, 54, 21))
self.label.setObjectName(_fromUtf8("label"))
self.pushButtonOK = QtGui.QPushButton(Dialog)
self.pushButtonOK.setGeometry(QtCore.QRect(150, 140, 75, 23))
self.pushButtonOK.setObjectName(_fromUtf8("pushButtonOK"))
self.pushButtonCancel = QtGui.QPushButton(Dialog)
self.pushButtonCancel.setGeometry(QtCore.QRect(240, 140, 75, 23))
self.pushButtonCancel.setObjectName(_fromUtf8("pushButtonCancel")) self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "Data:", None))
self.pushButtonOK.setText(_translate("Dialog", "Ok", None))
self.pushButtonCancel.setText(_translate("Dialog", "Cancel", None)) if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
新建MainPvalue.py 文件,代码如下:
# -*- coding: UTF8 -*- from PyQt4 import QtCore, QtGui
from parent import Ui_MainWindow
from child import Ui_Dialog
import sys
from PyQt4.QtGui import * class MainClass(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainClass, self).__init__(parent)
self.Ui = Ui_MainWindow()
self.Ui.setupUi(self)
self.setFixedSize(self.width(), self.height()) self.Ui.BtnOpenC.clicked.connect(self.Child) # 打开子窗体
def Child(self):
self.WChild = Ui_Dialog()
self.Dialog = QtGui.QDialog(self) # 不加self 不在父窗体中, 有两个任务栏 。 加self 表示在父子在窗体中在一个任务栏 self.WChild.setupUi(self.Dialog) self.WChild.pushButtonOK.clicked.connect(self.GetLine)
self.WChild.pushButtonCancel.clicked.connect(self.APPclose)
self.Dialog.exec_()
# 获取 弹框中填写的数据
def GetLine(self):
LineData=self.WChild.lineEdit.text()
self.Ui.textEdit.setText(LineData)
self.Dialog.close()
# 关闭当前弹框
def APPclose(self):
self.Dialog.close() if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MainApp = MainClass()
MainApp.show()
sys.exit(app.exec_())
运行MainPvalue.py
在父窗口定义一个子窗口的接口
self.child=None
然后实例子窗口赋给self.child 传递一个callback 函数
class MainForm(QDialog):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
self.child = None
self.table = QTableWidget()
self.table.setColumnCount(40)
self.table.setRowCount(30)
# set Column tab title
#self.table.setHorizontalHeaderLabels(list(range(5,10)))
for i in range(0, 5):
for x in range(0, 7):
item = QTableWidgetItem(str(i + x))
item.setTextAlignment(Qt.AlignLeft | Qt.AlignCenter)
item.setBackgroundColor(Qt.green)
self.table.setItem(x, i, item) lbutton = QPushButton("&L")
Vlayout = QHBoxLayout()
Vlayout.addWidget(lbutton)
Hlayout = QVBoxLayout()
Hlayout.addWidget(self.table)
Hlayout.addLayout(Vlayout)
self.setLayout(Hlayout)
self.resize(400,300)
self.setWindowTitle("Table")
self.connect(lbutton, SIGNAL("clicked()"), self.liveChange) def callback(self, c=0, r=0):
print('c=' + str(c) + 'r=' + str(r))
self.table.clear()
for i in range(0, c):
for x in range(0, r):
item = QTableWidgetItem(str(i + x))
item.setTextAlignment(Qt.AlignLeft | Qt.AlignCenter)
item.setBackgroundColor(Qt.red)
self.table.setItem(x, i, item) def liveChange(self):
if self.child == None:
self.child = liveDialog(self.callback, self) # self表示属于父类,LiveDialog继承父类
self.child.show()
self.child.raise_()
self.child.activateWindow() class liveDialog(QDialog):
def __init__(self, callback, parent=None):
super(liveDialog, self).__init__(parent)
self.callback = callback
self.c_edit = QLineEdit()
self.r_edit = QLineEdit()
layout = QHBoxLayout()
layout.addWidget(self.c_edit)
layout.addWidget(self.r_edit)
self.setLayout(layout)
self.connect(self.c_edit, SIGNAL("textChanged(QString)"), self.updateUi)
self.connect(self.r_edit, SIGNAL("textEdited(QString)"), self.updateUi) def updateUi(self, text):
c = self.c_edit.text()
r = self.r_edit.text()
if c and r:
self.callback(int(c), int(r)) if __name__ == "__main__":
app = QApplication(sys.argv)
mf = MainForm()
mf.show()
app.exec_()
++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++++++
恶搞弹框
顾名思义就是父类弹层若干个子窗体,将整个屏幕占满
clown.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>313</width>
<height>163</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>选择</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>查看</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>独立弹出任务栏</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
转换为py
clown.py:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'clown.ui'
#
# Created: Wed Mar 18 15:17:11 2015
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(313, 163)
self.verticalLayout = QtGui.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QtGui.QPushButton(Dialog)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1)
self.pushButton_3 = QtGui.QPushButton(Dialog)
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.gridLayout.addWidget(self.pushButton_3, 2, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout) self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton.setText(_translate("Dialog", "选择", None))
self.pushButton_2.setText(_translate("Dialog", "查看", None))
self.pushButton_3.setText(_translate("Dialog", "独立弹出任务栏", None)) if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
逻辑页面MainClown.py:
# -*- coding: UTF8 -*- from PyQt4 import QtCore, QtGui
from clown import Ui_Dialog
import sys
from PyQt4.QtGui import *
import ClownRcc
import random class MainClown(QtGui.QWidget):
def __init__(self, parent=None):
super(MainClown, self).__init__(parent)
self.Ui = Ui_Dialog()
self.Ui.setupUi(self)
self.setWindowIcon(QtGui.QIcon(':safe.ico'))
self.Ui.pushButton.clicked.connect(self.clickDig)
self.Ui.pushButton_2.clicked.connect(self.multDig)
self.Ui.pushButton_3.clicked.connect(self.taskDig) # 循环alert #--- 一个任务栏,一个dialog
def clickDig(self):
self.a=Icon(self)
self.a.setWindowFlags(QtCore.Qt.Window)
dictmsg = {1:u'海阔天空',2:u'心存梦想',3:u'勇往直前'}
for i in range(0, 5):
x = random.randint(100, 600)
y = random.randint(100, 600)
iskey = dictmsg.has_key(i)
if not iskey:
msg = u'确定要打开吗?'
else:
msg = dictmsg[i] OK = QtGui.QMessageBox.question(self, (u'提示'),(msg),QtGui.QMessageBox.Yes , QtGui.QMessageBox.No)
if OK == QtGui.QMessageBox.No:
return False
self.a.move(x, y)
self.a.show() # pos = self.previewWindow.pos() 随机self.previewWindow.move(pos) 到pos上 pos.setX(0) pos.setY(0) # 弹出另外一个任务栏 #--- 两个任务栏 关闭父窗体 子窗体不关闭
def taskDig(self):
self.b = Icon()
self.b.move(500, 600)
self.b.show() self.d = Icon()
self.d.move(600,700)
self.d.show() # 恶搞 #--- 多个任务栏 关闭父窗体 子窗体关闭 依赖于 closeEvent事件 QtCore.QCoreApplication.quit()
def multDig(self):
screenxy = QtGui.QDesktopWidget().screenGeometry() #计算屏幕分辨率
OK = QtGui.QMessageBox.warning(self, (u'提示'),(u'确定要打开吗?打开会后悔的!'),QtGui.QMessageBox.Yes , QtGui.QMessageBox.No)
if OK == QtGui.QMessageBox.Yes:
rounds = 30
vardict = {}
for ii in range(rounds):
vardict['a'+str(ii)] = 'a'+str(ii) for i in range(rounds):
self.adf=Icon()
setattr(self, vardict['a'+str(i)], Icon()) #第一个参数是对象,这里的self其实就是test.第二个参数是变量名,第三个是变量值 x = random.randint(20, screenxy.width())
y = random.randint(20, screenxy.height())
exec("self.a"+str(i)+".move("+str(x)+","+str(y)+")")
exec("self.a"+str(i)+".show()") # 重载关闭按钮 #--- 继承closeEvent ==QtCore.QCoreApplication.quit() 多个任务栏 关闭父窗体 子窗体也关闭
def closeEvent(self, event):
QtCore.QCoreApplication.quit() class Icon(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
palette1 = QtGui.QPalette(self)
palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap(':bluesky.jpg'))) # 设置背景图片
self.setPalette(palette1)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
mylayout = QVBoxLayout()
self.setLayout(mylayout) if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MainApp = MainClown()
MainApp.show()
sys.exit(app.exec_())