当前位置 : 主页 > 编程语言 > python >

PyQt4一个button对应一个slot(插槽)实例源码讲解

来源:互联网 收集:自由互联 发布时间:2022-06-18
#coding=utf8 ''' 这个实例中实现,一个slot连接两个或者多个信号,而且具有不同的slot行为。 这个实例中包括5个button和一个label,当其中任何一个button被点击, 信号与slot机制被用来更新
#coding=utf8
'''
这个实例中实现,一个slot连接两个或者多个信号,而且具有不同的slot行为。
这个实例中包括5个button和一个label,当其中任何一个button被点击,
信号与slot机制被用来更新label的文本信息。
把一个button的clicked()信号连接到一个响应信号的方法可能是最常见的连接场景。
'''
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class Form(QDialog):
def __init__(self,parent=None):
super(Form,self).__init__(parent)
#创建一个水平布局容器
#用来容纳button和label
hblayout=QHBoxLayout()
oneBtn=QPushButton("One")
twoBtn=QPushButton("Two")
threeBtn=QPushButton("Three")
fourBtn=QPushButton("Four")
fiveBtn=QPushButton("Five")
self.messageLabel=QLabel("No button is clicked")

hblayout.addWidget(oneBtn)
hblayout.addWidget(twoBtn)
hblayout.addWidget(threeBtn)
hblayout.addWidget(fourBtn)
hblayout.addWidget(fiveBtn)
hblayout.addWidget(self.messageLabel)

#链接按钮的信号与方法
self.connect(oneBtn, SIGNAL("clicked()"),self.one)
self.connect(twoBtn, SIGNAL("clicked()"),self.two)
self.connect(threeBtn, SIGNAL("clicked()"),self.three)
self.connect(fourBtn, SIGNAL("clicked()"),self.four)
self.connect(fiveBtn, SIGNAL("clicked()"),self.five)
#把水平容器放入Form中
self.setLayout(hblayout)

#创建点击信号发射时的响应函数
def one(self):
self.messageLabel.setText("You click button 'One'")

def two(self):
self.messageLabel.setText("You click button 'Two'")

def three(self):
self.messageLabel.setText("You click button 'Three'")

def four(self):
self.messageLabel.setText("You click button 'Four'")

def five(self):
self.messageLabel.setText("You click button 'Five'")

if __name__=="__main__":
app=QApplication(sys.argv)
form=Form()
form.show()
app.exec_()


程序运行截图:

PyQt4一个button对应一个slot(插槽)实例源码讲解_PyQt4C创建一个button按钮

PyQt4一个button对应一个slot(插槽)实例源码讲解_程序运行_02

PyQt4一个button对应一个slot(插槽)实例源码讲解_PyQt4C创建一个button按钮_03

PyQt4一个button对应一个slot(插槽)实例源码讲解_Pyqt4的button的clicked_04

PyQt4一个button对应一个slot(插槽)实例源码讲解_程序运行_05

PyQt4一个button对应一个slot(插槽)实例源码讲解_Pyqt4的button的clicked_06


网友评论