PyQt5 不同窗口之间的值传递

1.

from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSignal
import sys
"""
自定义对话框
"""


class MyDialog(QDialog):
    # 自定义信号
    mySignal = pyqtSignal(str)

    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.edit = QLineEdit(self)
        self.edit.move(10, 10)
        button = QPushButton('发送', self)
        button.move(10, 40)
        button.clicked.connect(self.sendEditContent)
        self.setWindowTitle('MyDialog')
        self.setGeometry(300, 300, 300, 200)

    def sendEditContent(self):
        content = self.edit.text()
        self.mySignal.emit(content)  # 发射信号


"""
主窗口
"""


class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()
        self.initUI()

    def initUI(self):
        self.button = QPushButton('open', self)
        self.button.clicked.connect(self.openMyDialog)
        self.button.move(10, 10)
        self.label = QLabel("hello", self)
        self.label.move(10, 50)
        self.setWindowTitle('Window')
        self.setGeometry(300, 300, 300, 200)
        self.show()

    def openMyDialog(self):
        my = MyDialog(self)
        # 在主窗口中连接信号和槽
        my.mySignal.connect(self.getDialogSignal)
        my.exec_()

    """
    实现槽函数
    """

    def getDialogSignal(self, connect):
        self.label.setText(connect)
if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = Window()
        w.show()
        print('父类窗口大小'+str(w.pos()))
        sys.exit(app.exec_())


2.

t3.py

from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSignal
import sys
from t4 import t4
class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()
        self.initUI()

    def initUI(self):
        self.button = QPushButton('open', self)

        self.button.clicked.connect(self.new)

        self.setWindowTitle('Window')
        self.resize(200,200)

    def new(self):
        my = t4(self)
        # 在主窗口中连接信号和槽
        my.mySignal.connect(self.setText)
        my.exec_()

    # 实现槽函数
    def setText(self, connect):
        self.button.setText(connect)

if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = Window()
        w.show()
        print('父类窗口大小'+str(w.pos()))
        sys.exit(app.exec_())

t4.py

from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSignal
import sys
"""
自定义对话框
"""


class t4(QDialog):
    # 自定义信号
    mySignal = pyqtSignal(str)

    def __init__(self, parent=None):
        super(t4, self).__init__(parent)
        self.initUI()

    def initUI(self):

        button = QPushButton('发送', self)
        button.move(10, 40)

        button.clicked.connect(self.sendText)

        self.setWindowTitle('MyDialog')
        self.setGeometry(300, 300, 300, 200)

    def sendText(self):
        self.mySignal.emit('test2')  # 发射信号

if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = t4()
        w.show()
        print('父类窗口大小'+str(w.pos()))
        sys.exit(app.exec_())

转:https://www.jianshu.com/p/60561380f789

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值