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

  1. C# winform窗体间传值(使用委托或事件)

    窗体间传值 今天得空,刚好看到网上好多人再找winform窗体间传值的问题,由于昨天项目的优化的感觉不错,就写了个C# winform窗体间传值的demo,希望能给需要的人的带来帮助: 工程的源代码地 ...

  2. 【转】WinForm窗体显示和窗体间传值

    以前对WinForm窗体显示和窗体间传值了解不是很清楚 最近做了一些WinForm项目,把用到的相关知识整理如下 A.WinForm中窗体显示 显示窗体可以有以下2种方法: Form.ShowDial ...

  3. C#利用事件与委托进行窗体间传值简单小例子

    本篇博客是利用C#委托与事件进行窗体间传值的简单小例子 委托与事件的详细解释大家可以参照张子阳的博客: http://www.tracefact.net/CSharp-Programming/Dele ...

  4. QT窗体间传值总结之Signal&Slot

    在写程序时,难免会碰到多窗体之间进行传值的问题.依照自己的理解,我把多窗体传值的可以使用的方法归纳如下: 1.使用QT中的Signal&Slot机制进行传值: 2.使用全局变量: 3.使用pu ...

  5. C#窗体间传值的简便方法/工具

    一.问题:窗体间传值必须需要窗体之间有联系,具体有如下方式 窗体间传值涉及到窗体A必须拥有窗体B,这样才可以实现A-B之间传值 窗体A与窗体B在窗体/实例C中,A-B可互相通讯 其他方式,不细讨论,复 ...

  6. winform 窗体间传值

    WinForm 两窗体之间传值实例 2010-12-27 22:10:11|  分类: 学业|举报|字号 订阅     下载LOFTER我的照片书  |     窗体Form1和Form2 Form2 ...

  7. ASP.NET 窗体间传值实现方法详解

    假设ParentForm.aspx 页面上有TextBox1文本框和Open按钮点击Open按钮弹出SubForm.aspx,SubForm.aspx页面上有TextBox1文本框和Close按钮点击 ...

  8. (C#)WinForm窗体间传值

      1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int v ...

  9. WinForm窗体间传值

    1.通过构造函数 特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中 int value1; string value2; public Form2 ( int val ...

随机推荐

  1. 粒子系统模块(Particle System Modules40)

    粒子系统模块(Particle System Modules40) 粒子系统模块(忍者飞镖) 粒子系统(忍者飞镖)(Particle System (Shuriken)) 用模块描述粒子一段时间内的行 ...

  2. ACM-括号匹配问题

    对ACM仰慕已久,无奈今天才开始.好吧,遇到的第二个题目就把我难到了.(实话是第一个) 进入正题,下面Copy出题目:  现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行输入一个数N(0 ...

  3. jQuery常用操作方法及常用函数总结

    一篇 jQuery 常用方法及函数的文章留存备忘. jQuery 常见操作实现方式 $("标签名") //取html元素 document.getElementsByTagName ...

  4. 如何用Wireshark捕获USB数据?

    现在越来越多的电子设备采用USB接口进行通讯,通讯标准也在逐步提高.那么,我们就会好奇这些设备是如何工作的?而无论你是一个硬件黑客,业余爱好者或者只是对它有一点兴趣的,USB对我们都是具有挑战性的. ...

  5. 2016年10月30日--JavaScript语法

    1.基本数据类型:字符串.小数.整数.日期时间.布尔型等. 2.变量:[var]定义变量:var a:所有变量定义都用var定义,var是通用的可变类型. 3.类型转换:转为整数:parseInt() ...

  6. 2.2---找链表倒数第K个结点

    答案,注意,一种是递归,另一种是迭代,那么巧妙利用双指针: 迭代: public static LinkedListNode nthToLast(LinkedListNode head, int n) ...

  7. PyQt4关闭最大化最小化取消双击最大化

    self.setWindowFlags(Qt.Window | Qt.WindowTitleHint | Qt.WindowCloseButtonHint | Qt.CustomizeWindowHi ...

  8. OpenMP求完数

    源代码: #include "stdafx.h" //必须写在首行,因为其前面的include都会被忽略 #include "omp.h" #include & ...

  9. Redis常用数据类型介绍、使用场景及其操作命令

    Redis常用数据类型介绍.使用场景及其操作命令 本文章同时也在cpper.info发布. Redis目前支持5种数据类型,分别是: 1.String(字符串) 2.List(列表) 3.Hash(字 ...

  10. show_sync_logs

    存入数据库的操作 CREATE TABLE `show_sync_logs` ( `id` ) NOT NULL AUTO_INCREMENT, `queue` ) DEFAULT NULL COMM ...