您尚未登录。

楼主 #1 2020-02-20 10:26:44

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

发一个 PyQt4/PyQt5/PySide2 都能跑的 Python 信号/槽 演示程序

from PyQt4.QtGui import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
from PyQt4.QtCore import pyqtSignal as Signal, pyqtSlot as Slot

# from PyQt5.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PyQt5.QtCore import pyqtSignal as Signal,pyqtSlot as Slot

# from PySide2.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PySide2.QtCore import Signal,Slot

import sys,os

class TestWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.count = 0

    #定义一个型号
    test_signal = Signal(int)

    #信号发送器
    def on_button_clicked(self):
       #self.close()
       self.count += 1
       self.test_signal.emit(self.count)

    # 信号接收器
    @Slot(int)
    def signal_receiver_test(self, value):
        self.editor.setText(str(value))

    def initUI(self):
        self.layout = QVBoxLayout()
        self.btn = QPushButton("信号发送测试(Qt Signal Test)", self)
        self.editor = QLineEdit("", self)
        self.layout.addWidget(self.btn)
        self.layout.addWidget(self.editor)
        self.setLayout(self.layout)
        self.btn.clicked.connect(self.on_button_clicked)
        self.test_signal.connect(self.signal_receiver_test)

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

一行一行敲出来的 ^_^


PyQt5/PySide2信号槽统一的代码参考了: https://www.learnpyqt.com/blog/pyqt5-vs-pyside2/

离线

楼主 #2 2020-02-20 10:32:37

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 发一个 PyQt4/PyQt5/PySide2 都能跑的 Python 信号/槽 演示程序

python_qt_signal_test.gif

离线

楼主 #3 2020-02-20 10:47:36

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 发一个 PyQt4/PyQt5/PySide2 都能跑的 Python 信号/槽 演示程序

# from PyQt4.QtGui import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PyQt4.QtCore import pyqtSignal as Signal, pyqtSlot as Slot

# from PyQt5.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PyQt5.QtCore import pyqtSignal as Signal,pyqtSlot as Slot

# from PySide2.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PySide2.QtCore import Signal,Slot

from Qt.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
from Qt.QtCore import Signal,Slot

import sys,os

class TestWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.count = 0

    #定义一个型号
    test_signal = Signal(int)

    #信号发送器
    def on_button_clicked(self):
       #self.close()
       self.count += 1
       self.test_signal.emit(self.count)

    # 信号接收器
    @Slot(int)
    def signal_receiver_test(self, value):
        self.editor.setText(str(value))

    def initUI(self):
        self.layout = QVBoxLayout()
        self.btn = QPushButton("信号发送测试(Qt Signal Test)", self)
        self.editor = QLineEdit("", self)
        self.layout.addWidget(self.btn)
        self.layout.addWidget(self.editor)
        self.setLayout(self.layout)
        self.btn.clicked.connect(self.on_button_clicked)
        self.test_signal.connect(self.signal_receiver_test)

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

也兼容 Qt.Py

^_^

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn