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

Python PyQt5中窗口数据传递的示例详解_第1张图片

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

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中窗口数据传递的示例详解_第2张图片

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

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

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中窗口数据传递的示例详解_第3张图片

到此这篇关于Python PyQt5中窗口数据传递的示例详解的文章就介绍到这了,更多相关PyQt5窗口数据传递内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Python PyQt5中窗口数据传递的示例详解)