Python PyQt5中窗口数据传递的示例详解

作者:SongYuLong的博客 时间:2023-12-30 10:14:43 

开发应用程序时,若只有一个窗口则只需关心这个窗口里面的各控件之间如何传递数据。如果程序有多个窗口,就要关心不同的窗口之间是如何传递数据。

单一窗口数据传递

对于单一窗口的程序来说,一个控件的变化会影响另一个控件的变化通过信号与槽的机制就可简单解决。

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_())

Python PyQt5中窗口数据传递的示例详解

多窗口数据传递:调用属性

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_())

Python PyQt5中窗口数据传递的示例详解

多窗口数据传递:信号与槽

子窗口发射信号,父窗口接收信号。

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_())

Python PyQt5中窗口数据传递的示例详解

来源:https://blog.csdn.net/songyulong8888/article/details/128113883

标签:Python,PyQt5,数据,传递
0
投稿

猜你喜欢

  • 在Dreamweaver MX中应用“占位图形”

    2009-07-10 13:16:00
  • Ubuntu权限不足无法创建文件夹解决方案

    2021-04-06 01:31:27
  • python数学建模之三大模型与十大常用算法详情

    2023-10-04 17:59:19
  • Python求两个文本文件以行为单位的交集、并集与差集的方法

    2021-12-25 09:12:18
  • asp如何用FSO对象显示一个文本文件?

    2010-06-09 18:41:00
  • PyQt5固定窗口大小的方法

    2021-05-18 12:34:43
  • MySQL binlog中的事件类型详解

    2024-01-16 03:21:25
  • python学习基础之循环import及import过程

    2022-04-02 13:23:25
  • 在Vue项目中使用Typescript的实现

    2024-04-26 17:39:57
  • 一个简单的ASP生成HTML分页程序

    2009-07-05 18:32:00
  • Python OpenCV阈值处理详解

    2023-10-07 19:38:47
  • python使用openCV遍历文件夹里所有视频文件并保存成图片

    2023-05-30 18:14:22
  • Python编程给numpy矩阵添加一列方法示例

    2023-08-29 07:22:30
  • PHP中过滤常用标签的正则表达式

    2024-05-03 15:35:43
  • vscode中使用Autoprefixer3.0无效的解决方法

    2023-10-05 11:03:46
  • Python图像处理之图像的灰度线性变换

    2021-12-16 22:30:58
  • Python中的布尔类型bool

    2023-08-11 13:10:00
  • Python调用百度AI实现颜值评分功能

    2023-07-30 22:53:40
  • 深入浅析python中的多进程、多线程、协程

    2022-06-05 15:59:27
  • python3模拟实现xshell远程执行linux命令的方法

    2022-05-26 11:45:14
  • asp之家 网络编程 m.aspxhome.com