网络编程
位置:首页>> 网络编程>> Python编程>> python GUI库图形界面开发之PyQt5信号与槽多窗口数据传递详细使用方法与实例

python GUI库图形界面开发之PyQt5信号与槽多窗口数据传递详细使用方法与实例

作者:jia666666  发布时间:2022-06-05 14:11:58 

标签:PyQt5,信号,槽

在pyqt5编程过程中,经常会遇到输入或选择多个参数的问题,把多个参数写到一个窗口中,主窗口会显得很臃肿,所以,一般是添加一个按钮,调用对话框,在对话框中进行参数的选择,关闭对话框将参数返回给主窗口

pyqt提供了一些标准的对话框类,用于输入数据,修改数据,更改应用的设置等,常见的有QFileDialog,QInputDialog,QColorDialog, QFontDialog等,在不同的窗口之间传参数有两种常用的方式,一种在自定义对话框之间通过属性传参,另一种在窗口之间使用信号与槽机制传参,这里主要介绍第一种

单一窗口的数据传递实例

对于具有单一窗口的程序来说,一个控件的变化会影响另一个控件的变化,这中变化利用信号与槽的关系非常容易解决


import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class WinForm(QWidget):
 def __init__(self):
   super(WinForm, self).__init__()
   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)
   #设置滑块数值信号改变连接Lcd的更新
   slider.valueChanged.connect(lcd.display)
   #设置初始位置以及初始大小,设置标题
   self.setGeometry(300,300,350,150)
   self.setWindowTitle('信号与槽:连接滑块LCd')
if __name__ == '__main__':
 app=QApplication(sys.argv)
 form=WinForm()
 form.show()
 sys.exit(app.exec_())

运行效果如图

python GUI库图形界面开发之PyQt5信号与槽多窗口数据传递详细使用方法与实例

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

新建对话框子窗口属性,我这里的名字为: jia_13多窗口数据传递之调用属性


from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class DateDialog(QDialog):
 def __init__(self,parent=None):
   super(DateDialog, self).__init__(parent)
   self.setWindowTitle('DateDialog')
   #在布局中添加控件
   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()
 @staticmethod
 def getDateTime(parent=None):
   dialog=DateDialog(parent)
   result=dialog.exec_()
   date=dialog.dateTime()
   return (date.date(),date.time(),result==QDialog.Accepted)

再新建一个主窗口文件,用来调用上一个子窗口


import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from jia_13多窗口数据传递之调用属性 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.onButton1Clicked)
   self.button2=QPushButton('弹出对话框2')
   self.button2.clicked.connect(self.onButton2Clicked)
   gridLayout=QGridLayout(self)
   gridLayout.addWidget(self.lineEdit)
   gridLayout.addWidget(self.button1)
   gridLayout.addWidget(self.button2)
 def onButton1Clicked( 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)
 def onButton2Clicked( 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' %result)
if __name__ == '__main__':
 app=QApplication(sys.argv)
 form=WinForm()
 form.show()
 sys.exit(app.exec_())

运行程序,显示如图

python GUI库图形界面开发之PyQt5信号与槽多窗口数据传递详细使用方法与实例

代码分析

在主窗口调用对话框有两种方法,本例中,这两种方法操作效果是一样的,都需要点击弹出对话框按钮,在对话框的时间日期控件中选择日期,则会把选中的日期返回到主窗口的lineTeXt文本控件中

第一中方法:直接在主窗口程序中实例化该对话框,然后调用该对话框的函数,来获取返回值,根据对话框的返回值单击确认按钮还是取消按钮来进行下一步的操作,同理,对于上面的DateDialog,主窗口程序代码如下

 def onButton1Clicked( self ):

        dialog=DateDialog(self)

        result=dialog.exec_()

        date=dialog.dateTime()

        self.lineEdit.setText(date.date().toString())

第二种方法:在主窗口程序中调用子窗口的静态函数,实际上这种方法与第一种方法是一样的,只不过他利用静态函数的特点,在子窗口的静态函数中的创建实例化对象

 def onButton2Clicked( self ):

        date,time,result=DateDialog.getDateTime()

        self.lineEdit.setText(date.toString())

多窗口数据传递信号与槽实例

对于多窗口的数据传递,一般是通过子窗口发射信号的,主窗口通过槽函数捕获这个信号,然后获取信号里面的数据,子窗口发射的信号有两种:一种是发射内置pyqt信号,一种是发射自定义的信号

发射自定义的信号,好处是它的参数可以自定义,可以为int list dict等各种类型与多个参数

新建一个子对话框文件,这里的名称为:jia_14多窗口信号传递之信号与槽


# -*- coding: utf-8 -*-
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_emit = QDateTimeEdit(self)
   self.datetime_emit.setCalendarPopup(True)
   self.datetime_emit.setDateTime(QDateTime.currentDateTime())
   layout.addWidget(self.label)
   layout.addWidget(self.datetime_inner)
   layout.addWidget(self.datetime_emit)
   # 使用两个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_emit.dateTimeChanged.connect(self.emit_signal)
 def emit_signal(self):
   date_str = self.datetime_emit.dateTime().toString()
   self.Signal_OneParameter.emit(date_str)

创建主窗口,调用对话框文件


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from jia_14多窗口信号传递之信号与槽 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):
   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 GUI库图形界面开发之PyQt5信号与槽多窗口数据传递详细使用方法与实例

代码分析

下面代码表示,当空间datetime_emit的时间发生变化时,就会触发,子窗口的槽函数emit_signal,而在这个槽函数中又会发射自定义信号Signal_OneParameter,这个信号函数是为了传递date_str参数给主函数

    def emit_signal(self):

        date_str = self.datetime_emit.dateTime().toString()

        self.Signal_OneParameter.emit(date_str)

对于主窗口,关键是获取子窗口的信号,并把它绑定在自己的槽函数上,这样就实现了子窗口的控件与主窗口控件的绑定,核心代码如下

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

本文主要讲解了PyQt5窗口数据传递与PyQt5信号与槽多窗口数据传递详细使用方法与实例,更多关于PyQt5信号与槽的知识请查看下面的相关链接

来源:https://blog.csdn.net/jia666666/article/details/81670569

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com