当前位置:   article > 正文

PyQt5 使用 pyqtSignal 进行窗口数据传递_pyqt5 pyqtsignal

pyqt5 pyqtsignal

示例1

dialog01.py
import sys
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QPushButton, QApplication, QWidget, QLineEdit
from PyQt5.QtCore import Qt
from dialog01 import Dialog


class Wind(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(400, 500)
        layout = QVBoxLayout()
        self.button1 = QPushButton('button1')
        self.button1.clicked.connect(self.show_dialog)
        self.edit = QLineEdit()

        widget = QWidget()
        layout.addWidget(self.button1)
        layout.addWidget(self.edit)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def name_edit(self, name):
        self.edit.setText(name)

    def show_dialog(self):
        print('show_dialog')
        dialog = Dialog(self)
        dialog.update_name.connect(self.name_edit)
        dialog.setWindowModality(Qt.WindowModal)
        dialog.show()
        print('end')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Wind()
    win.show()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
dialog01.py
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QApplication
from PyQt5.QtCore import pyqtSignal
import sys


class Dialog(QDialog):
    update_name = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QVBoxLayout()
        label = QLabel('name')
        layout.addWidget(label)
        self.line_edit = QLineEdit()
        self.line_edit.setPlaceholderText('input name')
        self.line_edit.textChanged.connect(self.emit_name)
        layout.addWidget(self.line_edit)
        self.setLayout(layout)

    def emit_name(self):
        name = self.line_edit.text()
        self.update_name.emit(name)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
效果

在这里插入图片描述

示例2

dialog01.py
import sys
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QPushButton, QApplication, QWidget, QLineEdit
from PyQt5.QtCore import Qt
from dialog01 import Dialog


class Wind(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(400, 500)
        layout = QVBoxLayout()
        self.button1 = QPushButton('button1')
        self.button1.clicked.connect(self.show_dialog)
        self.edit = QLineEdit()

        widget = QWidget()
        layout.addWidget(self.button1)
        layout.addWidget(self.edit)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def show_dialog(self):
        print('show_dialog')
        dialog = Dialog(self)
        dialog.setWindowModality(Qt.WindowModal)
        dialog.show()
        print('end')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Wind()
    win.show()
    sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
dialog01.py
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QApplication
from PyQt5.QtCore import pyqtSignal
import sys


class Dialog(QDialog):
    update_name = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        label = QLabel('name')
        layout.addWidget(label)
        self.line_edit = QLineEdit()
        self.line_edit.setPlaceholderText('input name')
        self.line_edit.textChanged.connect(self.emit_name)
        layout.addWidget(self.line_edit)
        self.setLayout(layout)

    def emit_name(self):
        name = self.line_edit.text()
        self.parent.edit.setText(name)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/838883
推荐阅读
相关标签
  

闽ICP备14008679号