Pyqt 窗体间传值
窗体间传值网上有好多方法,比如新建文件,先将子类窗体的数据传到文件中,父窗体读取文件、 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_())

Pyqt 窗体间传值的更多相关文章
- C# winform窗体间传值(使用委托或事件)
		
窗体间传值 今天得空,刚好看到网上好多人再找winform窗体间传值的问题,由于昨天项目的优化的感觉不错,就写了个C# winform窗体间传值的demo,希望能给需要的人的带来帮助: 工程的源代码地 ...
 - 【转】WinForm窗体显示和窗体间传值
		
以前对WinForm窗体显示和窗体间传值了解不是很清楚 最近做了一些WinForm项目,把用到的相关知识整理如下 A.WinForm中窗体显示 显示窗体可以有以下2种方法: Form.ShowDial ...
 - C#利用事件与委托进行窗体间传值简单小例子
		
本篇博客是利用C#委托与事件进行窗体间传值的简单小例子 委托与事件的详细解释大家可以参照张子阳的博客: http://www.tracefact.net/CSharp-Programming/Dele ...
 - QT窗体间传值总结之Signal&Slot
		
在写程序时,难免会碰到多窗体之间进行传值的问题.依照自己的理解,我把多窗体传值的可以使用的方法归纳如下: 1.使用QT中的Signal&Slot机制进行传值: 2.使用全局变量: 3.使用pu ...
 - C#窗体间传值的简便方法/工具
		
一.问题:窗体间传值必须需要窗体之间有联系,具体有如下方式 窗体间传值涉及到窗体A必须拥有窗体B,这样才可以实现A-B之间传值 窗体A与窗体B在窗体/实例C中,A-B可互相通讯 其他方式,不细讨论,复 ...
 - winform 窗体间传值
		
WinForm 两窗体之间传值实例 2010-12-27 22:10:11| 分类: 学业|举报|字号 订阅 下载LOFTER我的照片书 | 窗体Form1和Form2 Form2 ...
 - ASP.NET 窗体间传值实现方法详解
		
假设ParentForm.aspx 页面上有TextBox1文本框和Open按钮点击Open按钮弹出SubForm.aspx,SubForm.aspx页面上有TextBox1文本框和Close按钮点击 ...
 - (C#)WinForm窗体间传值
		
1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int v ...
 - WinForm窗体间传值
		
1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int val ...
 
随机推荐
- Linux 查看磁盘空间大小
			
(1)查看文件大小 1. 查看当前文件夹下所有文件大小(包括子文件夹) du -sh 2.查看var目录下文件大小 du -sh var 3.查看指定文件夹下所有文件大小(包括子文件 ...
 - Java验证码识别解决方案
			
建库,去重,切割,识别. package edu.fzu.ir.test; import java.awt.Color; import java.awt.image.BufferedImage; im ...
 - CentOS 7 运行级别切换
			
CentOS 7 之前的版本是通过 /etc/inittab 文件来定义系统运行级别: [sywu@wusuyuan ~]$ cat /etc/centos-release CentOS releas ...
 - 三大UML建模工具Visio、Rational Rose、PowerDesign的区别
			
本文源自http://developer.51cto.com/art/201006/207993.htm UML建模工具Visio .Rational Rose.PowerDesign的比较 RO ...
 - 如何区分SNAT和DNAT
			
从定义来讲它们一个是源地址转换,一个是目标地址转换.都是地址转换的功能,将私有地址转换为公网地址.要区分这两个功能可以简单的由连接发起者是谁来区分: 内部地址要访问公网上的服务时(如web ...
 - C#之常见数组编码错误
			
摘抄自C#本质论(第四版,P55) 常见错误 错误描述 改正后的代码 int numbers[] 用于声明数组的方括号放在数据类型之后,而不是在变量标识符之后 int[] numbers; int[] ...
 - mysql中表名是order的CRUD的错误
			
org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: com.mysql.jdbc. ...
 - iso网络各层协议
			
(1)网卡的作用就是把数据进行串并转换(串连数据是比特流形式的,存在与本计算机内部,而计算机与计算机之间是通过帧形式的数据来进行数据传输的),MAC子层规定了如何在物理线路上传输的frame,LLC的 ...
 - 转:C++编程隐蔽错误:error C2533: 构造函数不能有返回类型
			
C++编程隐蔽错误:error C2533: 构造函数不能有返回类型 今天在编写类的时候,出现的错误. 提示一个类的构造函数不能够有返回类型.在cpp文件里,该构造函数定义处并没有返回类型.在头文件里 ...
 - linux学习之lvm-逻辑卷管理器
			
一.简介 lvm即逻辑卷管理器(logical volume manager),它是linux环境下对磁盘分区进行管理的一种机制.lvm是建立在硬盘和分区之上的一个逻辑层,来提高分区管理的灵活性.它是 ...