Python PyQt5中窗口数据传递的示例详解
作者:SongYuLong的博客 发布时间:2023-12-30 10:14:43
标签:Python,PyQt5,数据,传递
开发应用程序时,若只有一个窗口则只需关心这个窗口里面的各控件之间如何传递数据。如果程序有多个窗口,就要关心不同的窗口之间是如何传递数据。
单一窗口数据传递
对于单一窗口的程序来说,一个控件的变化会影响另一个控件的变化通过信号与槽的机制就可简单解决。
import sys
from PyQt5.QtWidgets import QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication
from PyQt5.QtCore import Qt
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.initUI()
def initUI(self):
# 先创建滑块和LCD控件
lcd = QLCDNumber(self)
slider = QSlider(Qt.Horizontal, self)
vBox = QVBoxLayout()
vBox.addWidget(lcd)
vBox.addWidget(slider)
self.setLayout(vBox)
# valueChanged()是QSlider的一个信号函数,只要slider的值发生改变,就会发射一个信号
# 然后通过connect连接信号的接收控件lcd
slider.valueChanged.connect(lcd.display)
# lcd.setBinMode()
# lcd.setHexMode()
# lcd.setDecMode()
# lcd.setOctMode()
# lcd.setNumDigits(2)
# lcd.setDigitCount(4)
# lcd.setSegmentStyle(QLCDNumber.Filled)
self.setGeometry(300, 300, 350, 150)
self.setWindowTitle("信号与槽:连接滑块LCD")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = WinForm()
win.show()
sys.exit(app.exec_())
多窗口数据传递:调用属性
DateDialog.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DateDialog(QDialog):
def __init__(self, parent=None):
super(DateDialog, self).__init__(parent)
self.setWindowTitle("DataDialog")
# 在布局中添加控件
layout = QVBoxLayout(self)
self.datetime = QDateTimeEdit(self)
self.datetime.setCalendarPopup(True)
self.datetime.setDateTime(QDateTime().currentDateTime())
layout.addWidget(self.datetime)
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
# 从对话框获取当前日期时间
def dateTime(self):
return self.datetime.dateTime()
# 使用静态函数创建对话框并返回(date, time, accepted)
@staticmethod
def getDateTime(parent=None):
dialog = DateDialog(parent)
result = dialog.exec_()
date = dialog.dateTime()
return (date.date(), date.time(), result == QDialog.Accepted)
CallDialogMainWin.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from DateDialog import DateDialog
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.resize(400, 90)
self.setWindowTitle("对话框关闭时返回值给主窗口")
self.lineEdit = QLineEdit(self)
self.button1 = QPushButton("弹出对话框1")
self.button1.clicked.connect(self.onButton1Click)
self.button2 = QPushButton("弹出对话框2")
self.button2.clicked.connect(self.onButton2Click)
gridLayout = QGridLayout()
gridLayout.addWidget(self.lineEdit)
gridLayout.addWidget(self.button1)
gridLayout.addWidget(self.button2)
self.setLayout(gridLayout)
def onButton1Click(self):
dialog = DateDialog(self)
result = dialog.exec_()
date = dialog.dateTime()
self.lineEdit.setText(date.date().toString())
print('\n日期对话框的返回值')
print('date=%s' % str(date.date()))
print('time=%s' % str(date.time()))
print('result=%s' % result)
dialog.destroy()
def onButton2Click(self):
date, time, result = DateDialog.getDateTime()
self.lineEdit.setText(date.toString())
print('\n日期对话框返回值')
print('date=%s' % str(date))
print('time=%s' % str(time))
print('result=%s' % str(result))
if __name__ == "__main__":
app = QApplication(sys.argv)
win = WinForm()
win.show()
sys.exit(app.exec_())
多窗口数据传递:信号与槽
子窗口发射信号,父窗口接收信号。
DateDialog2.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DateDialog(QDialog):
Signal_OneParameter = pyqtSignal(str)
def __init__(self, parent=None):
super(DateDialog, self).__init__(parent)
self.setWindowTitle("子窗口:用来发射信号")
# 在布局中添加控件
layout = QVBoxLayout(self)
self.label = QLabel(self)
self.label.setText("前者发射内置信号\n后者发射自定义信号")
self.datetime_inner = QDateTimeEdit(self)
self.datetime_inner.setCalendarPopup(True)
self.datetime_inner.setDateTime(QDateTime.currentDateTime())
self.datetime_eimt = QDateTimeEdit(self)
self.datetime_eimt.setCalendarPopup(True)
self.datetime_eimt.setDateTime(QDateTime.currentDateTime())
layout.addWidget(self.label)
layout.addWidget(self.datetime_inner)
layout.addWidget(self.datetime_eimt)
# 使用两个button(Ok和Cancel)分别连接accept()和reject()槽函数
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
self.datetime_eimt.dateTimeChanged.connect(self.emit_signal)
def emit_signal(self):
date_str = self.datetime_eimt.dateTime().toString()
self.Signal_OneParameter.emit(date_str)
CallDialogMainWin2.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from DateDialog2 import DateDialog
class WinForm (QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.resize(400, 90)
self.setWindowTitle("信号与槽传递参数的示例")
self.open_btn = QPushButton("获取时间")
self.lineEdit_inner = QLineEdit(self)
self.lineEdit_emit = QLineEdit(self)
self.open_btn.clicked.connect(self.openDialog)
self.lineEdit_inner.setText("接收子窗口内置信号的时间")
self.lineEdit_emit.setText("接收子窗口自定义信号的时间")
grid = QGridLayout()
grid.addWidget(self.lineEdit_inner)
grid.addWidget(self.lineEdit_emit)
grid.addWidget(self.open_btn)
self.setLayout(grid)
def openDialog(self):
dialog = DateDialog(self)
'''连接子窗口的内置信号与主窗口的槽函数'''
dialog.datetime_inner.dateTimeChanged.connect(self.deal_inner_slot)
'''连接子窗口的自定义信号与主窗口的槽函数'''
dialog.Signal_OneParameter.connect(self.deal_emit_slot)
dialog.show()
def deal_inner_slot(self, date):
print(date)
self.lineEdit_inner.setText(date.toString())
def deal_emit_slot(self, dateStr):
self.lineEdit_emit.setText(dateStr)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())
来源:https://blog.csdn.net/songyulong8888/article/details/128113883
0
投稿
猜你喜欢
- 本文实例为大家分享了Python函数式编程实现登录注册功能的具体代码,供大家参考,具体内容如下代码:def login(username,
- 前言本文主要给大家介绍了关于Yii2结合Workerman的websocket的相关内容,两者都是好东西,我就想着能不能结合起来,这样Yii
- 写任何编程代码,不同的开发者都会有不同的见解。但参考一下总是好的,下面是来自 Javascript Toolbox 发布的 14条最佳JS代
- 单例模式是一个经典设计模式,简要的说,一个类的单例模式就是它只能被实例化一次,实例变量在第一次实例化时就已经固定。 在Python
- IIS设置首先打开IIS就不用说了巴第2步右建默认网站属性第3步主目录选项卡点击第3步的配置按钮弹出窗口的设置,至此IIS设置完毕任意编辑器
- 前言今天我们一起来聊聊DataFrame中的索引。上一篇文章当中我们介绍了DataFrame数据结构当中一些常用的索引的使用方法,比如ilo
- 英文文档:class complex([real[, imag]])Return a complex number with the val
- 如下所示:from numpy import *import numpy as npimport matplotlib.pyplot as
- 1.问题背景Python之所以强大,不仅是因为该语言本身的特点,也是因为它拥有众多无所不能的第三方库。强大的软件库允许开发者专注于业务,避免
- 前言 随机数可以用于数学,游戏,安全等领域中,还经常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。平时数据分析各种分布的数据构造也
- 最近在做cocos2d-x的简明配置,发现有的朋友的文本编辑器,自动将\r\n截断成\n,(在unix上换行使用\n,windows上,换行
- 1.本人第一次学python做出来的,当时满满的成就感,当作纪念!!!!!非常简单,复制即可使用代码块import json#把字符串类型的
- 数据概况Fashion-mnist经典的MNIST数据集包含了大量的手写数字。十几年来,来自机器学习、机器视觉、人工智能、深度学习领域的研究
- 而今天我们要说的内容是:如果在网页中存在文件资源,如:图片,电影,文档等。怎样通过Python爬虫把这些资源下载下来。1、怎样在网上找资源:
- 本文实例讲述了python判断字符串是否包含子字符串的方法。分享给大家供大家参考。具体如下:python的string对象没有contain
- Python两个内置函数——locals 和globals这两个函数主要提供,基于字典的访问局部和全局变量的方式。在理解这两个函数时,首先来
- 前言嗨嗨,大家晚上好 ~ 又来给你们分享小妙招啦在python列表有重复元素时,可以有以下几种方式进行删除觉得不错的话,赶紧学起来用用吧 !
- 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性。先定义cl
- 首先先安装好我们的Python 这里就不多介绍了1.打开python.exe:点击File 下的小扳手图标 Settings…2.打开设置界
- 导言在前面的教程里我们学习了DataList提供了一些风格样式的属性.而且我们还学习了如何定义HeadStyle, ItemStyle, A