The sender is an object that sends a signal. The receiver is the object that receives the signal. The slot is the method that reacts to the signal.
event target 发送一个信号,信号接收器接收信号,槽专门用来处理这个信号。
代码:
# coding='utf-8'
from PyQt5.QtWidgets import QWidget, QLCDNumber,\
QSlider, QVBoxLayout, QApplication
from PyQt5.QtCore import Qt
import sys
class Gui(QWidget):
def __init__(self):
super(Gui, self).__init__()
self.start()
def start(self):
# 创建QWidget上的计数器
lcd = QLCDNumber(self)
# 创建水平放置在QWidget上的滑条
sld = QSlider(Qt.Horizontal, self)
# 创建垂直方向的布局控件
vbox = QVBoxLayout()
# 给这个垂直方向的布局控件添加两个子组件
vbox.addWidget(lcd)
vbox.addWidget(sld)
# 设置QWidget窗体的布局为垂直方向布局vbox
self.setLayout(vbox)
# 将sld滑条的数值改变与lcd数字显示联系在一起
# 其中, sld是event source, valueChanged是event target,
# lcd.display是slot, QWidget是event object
sld.valueChanged.connect(lcd.display)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal and slot')
self.show()
win = QApplication(sys.argv)
gui = Gui()
sys.exit(win.exec_())
运行结果:
image.png
网友评论