PyQt5使用多个窗口传递数据的2种方法

1、通过窗口类的属性来传递参数

# 窗口之间数据传递(通过属性来进行消息传递)
from PyQt5.QtWidgets import QDialogButtonBox, QDateTimeEdit, QDialog, QComboBox, QTableView, QAbstractItemView, \
    QHeaderView, QTableWidget, QTableWidgetItem, QMessageBox, QListWidget, QListWidgetItem, QStatusBar, QMenuBar, QMenu, \
    QAction, QLineEdit, QStyle, QFormLayout, QVBoxLayout, QWidget, QApplication, QHBoxLayout, QPushButton, QMainWindow, \
    QGridLayout, QLabel
from PyQt5.QtGui import QIcon, QPixmap, QStandardItem, QStandardItemModel, QCursor, QFont, QBrush, QColor, QPainter, \
    QMouseEvent, QImage, QTransform
from PyQt5.QtCore import QStringListModel, QAbstractListModel, QModelIndex, QSize, Qt, QObject, pyqtSignal, QTimer, \
    QEvent, QDateTime, QDate

import sys


class Win(QWidget):
    def __init__(self, parent=None):
        super(Win, self).__init__(parent)
        self.resize(400, 400)

        self.btn = QPushButton("按钮", self)
        self.btn.move(50, 50)
        self.btn.setMinimumWidth(80)


        self.label = QLabel('显示信息', self)
        self.label.setMinimumWidth(420)

        self.btn.clicked.connect(self.fn)   # 按钮绑定槽函数

    # 显示子窗口传来的日期字符串或者其他数据
    def fn(self):
        # 调用静态方法,无需实例化
        date, time, res = Child_Dialog.getResult(self)
        print(date, time, res)
        # 直接实例化自定义的对话框类
        # dialog=Child_Dialog()
        # res=dialog.exec_()	# 阻塞运行,直到窗口关闭
        # date=dialog.datetime.date()
        # time=dialog.datetime.time()
        # print(res,date,time)


# 弹出框对象
class Child_Dialog(QDialog):

    def __init__(self, parent=None):
        super(Child_Dialog, self).__init__(parent)
        layout = QVBoxLayout(self)
        self.label = QLabel(self)
        self.datetime = QDateTimeEdit(self)
        self.datetime.setCalendarPopup(True)
        self.datetime.setDateTime(QDateTime.currentDateTime())
        self.label.setText("请选择日期")
        layout.addWidget(self.label)
        layout.addWidget(self.datetime)

        buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
        buttons.accepted.connect(self.accept)  # 点击ok,该方法默认存在
        buttons.rejected.connect(self.reject)  # 点击cancel,该方法默认存在
        layout.addWidget(buttons)

    # 该方法在父类方法中调用,直接打开了子窗体,返回值则用于向父窗体数据的传递
    @staticmethod
    def getResult(self, parent=None):
        dialog = Child_Dialog(parent)
        result = dialog.exec_()
        d = dialog.datetime.dateTime()
        return (d.date(), d.time(), result)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Win()
    win.show()
    sys.exit(app.exec_())

2、窗口之间使用自定义信号和槽机制传参

#窗口之间数据传递(通过属性方式)
from PyQt5.QtWidgets import QDialogButtonBox, QDateTimeEdit,QDialog,QComboBox,QTableView,QAbstractItemView,QHeaderView,QTableWidget, QTableWidgetItem, QMessageBox,QListWidget,QListWidgetItem, QStatusBar,  QMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout,   QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel
from PyQt5.QtGui import QIcon,QPixmap,QStandardItem,QStandardItemModel,QCursor,QFont,QBrush,QColor,QPainter,QMouseEvent,QImage,QTransform
from PyQt5.QtCore import QStringListModel,QAbstractListModel,QModelIndex,QSize,Qt,QObject,pyqtSignal,QTimer,QEvent,QDateTime,QDate

import sys
class Win(QWidget):
    def __init__(self,parent=None):
        super(Win, self).__init__(parent)
        self.resize(400,400)

        self.btn=QPushButton("按钮",self)
        self.btn.move(50,50)
        self.btn.setMinimumWidth(120)
        self.btn.clicked.connect(self.openDialog)   # 按钮绑定槽函数,用于打开子窗口

        self.label=QLabel('这里显示信息',self)
        self.label.setMinimumWidth(420)

    #打开Dialog
    def openDialog(self):
        dialog=Child_Dialog(self)
        #连接【子窗口内置消息和主窗口的槽函数】
        dialog.datetime.dateChanged.connect(self.slot_inner)    # 日历控件自带的信号
        #连接【子窗口自定义消息和主窗口槽函数】
        dialog.dialogSignel.connect(self.slot_emit)             # 子窗口的自定义消息,用于传递参数
        dialog.show()       # 非阻塞
        # dialog.exec_()        # 阻塞
    def slot_inner(self,date):
        print("主窗口:method_1")
        self.label.setText("①"+str(date)+">>内置消息获取日期为")


    def slot_emit(self,flag,str):
        print("主窗口:method_2")
        print(flag)
        if flag=="0":#点击ok
          self.label.setText("②"+str(str)+">>自定义消息")
        else:#点击cancel
          self.label.setText(str)

#弹出框对象
class Child_Dialog(QDialog):
    #自定义消息
    dialogSignel=pyqtSignal(int,str)

    def __init__(self,parent=None):
        super(Child_Dialog, self).__init__(parent)
        layout=QVBoxLayout(self)
        self.label=QLabel(self)
        self.datetime=QDateTimeEdit(self)
        self.datetime.setCalendarPopup(True)
        self.datetime.setDateTime(QDateTime.currentDateTime())
        self.label.setText("请选择日期")
        layout.addWidget(self.label)
        layout.addWidget(self.datetime)

        buttons=QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel,Qt.Horizontal,self)
        buttons.accepted.connect(self.accept)#点击ok
        buttons.rejected.connect(self.reject)#点击cancel
        layout.addWidget(buttons)
    def accept(self):#点击ok是发送内置信号
        print("accept")
        self.dialogSignel.emit(0,self.datetime.text())
        self.destroy()
    def reject(self):#点击cancel时,发送自定义信号
        print('reject')
        self.dialogSignel.emit(1,"清空")
        self.destroy()


if __name__=='__main__':

    app=QApplication(sys.argv)
    win = Win()
    win.show()
    sys.exit(app.exec_())

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5是一个强大的Python GUI框架,可以用于创建多窗口应用程序。同时,它也提供了多线程的支持,可以在应用程序中同时执行多个任务。 对于多窗口应用程序,可以使用PyQt5中的QMainWindow类创建主窗口,并使用其他QWidget类创建额外的窗口。主窗口是整个应用程序的主要界面,而窗口可以用于显示其他视图或执行其他任务。 使用多线程可以使应用程序具有更好的响应性能,特别是在处理复杂的或耗时的任务时。可以使用PyQt5中的QThread类创建线程,然后将任务分配给这些线程进行并行执行,以避免主线程被阻塞。 在多窗口应用程序中使用多线程时,需要注意以下几点。首先,应该避免使用全局变量,而应该使用线程间通信机制进行数据传递PyQt5提供了信号与槽机制和队列等线程间通信的方式。 其次,需要注意线程安全性。多个线程共享相同的数据时,可能会引起数据竞争和不一致性。可以使用互斥锁等同步机制来保证数据的一致性。 此外,还需要注意界面的更新。在多线程中进行的任务会影响到界面的显示,但需要注意将界面的更新操作放在主线程中进行,以避免多个线程同时更新界面而导致的冲突。 总之,PyQt5提供了多窗口和多线程的支持,可以帮助我们创建功能强大的应用程序,提升用户体验和性能。但在使用过程中,需要注意线程间通信、线程安全性和界面更新等问题,以保证应用程序的稳定和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值