Sometimes it is convenient to know which widget is the sender of a signal. For this, PyQt4 has thesender() method.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we determine the event sender
object. author: Jan Bodnar
website: zetcode.com
last edited: October 2011
""" import sys
from PyQt4 import QtGui, QtCore class Example(QtGui.QMainWindow): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): btn1 = QtGui.QPushButton("Button 1", self)
btn1.move(30, 50) btn2 = QtGui.QPushButton("Button 2", self)
btn2.move(150, 50) btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked) self.statusBar() self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Event sender')
self.show() def buttonClicked(self): sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed') def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

We have two buttons in our example. In the buttonClicked() method we determine which button we have clicked by calling the sender() method.

btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)

Both buttons are connected to the same slot.

def buttonClicked(self):

    sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')

We determine the signal source by calling the sender() method. In the statusbar of the application, we show the label of the button being pressed.

Figure: Event sender

Event sender的更多相关文章

  1. Yii2的深入学习--事件Event

    我们先来看下事件在 Yii2 中的使用,如下内容摘自 Yii2中文文档 事件可以将自定义代码“注入”到现有代码中的特定执行点.附加自定义代码到某个事件,当这个事件被触发时,这些代码就会自动执行.例如, ...

  2. pyqt sender()学习

    #!/usr/bin/python # -*- coding: utf-8 -*- # sender.py import sys from PyQt4 import QtGui, QtCore cla ...

  3. Yii2基本概念之——事件(Event)

    说起事件(event),我们可是一点都不陌生.现实生活当中的事件无处不在,比如你发了一条微博,触发了一条事件,导致关注你的人收到了一条消息,看到你发的内容:比如你通过支付宝买东西,付了款,触发一个事件 ...

  4. XAML实例教程系列 - 事件(Event) 五

    Kevin Fan分享开发经验,记录开发点滴 XAML实例教程系列 - 事件(Event) 2012-06-19 01:36 by jv9, 1727 阅读, 7 评论, 收藏, 编辑 Events, ...

  5. C# delegate和C# event

    在基于Windows平台的程序设计中,事件(event)是一个很重要的概念.因为在几乎所有的Windows应用程序中,都会涉及大量的异步调用,比如响应点击按钮.处理Windows系统消息等,这些异步调 ...

  6. PyQt4入门学习笔记(四)

    在PyQt4中的事件和信号 事件 所有的GUI应用都是事件驱动的.事件主要是来自于应用的使用者,但是像互联网连接,窗口管理器或者计时器也可以产生事件.当我们调用应用的exec_()方法时,应用就进入了 ...

  7. Electron Angular 开发小记

    一介绍 electron分为主进程和渲染进程,主进程负责和原生交互,控制窗口等. 渲染进程就是普通网页.主进程和渲染进程可以通过ipcMain(主进程使用)及ipcRenderer(渲染进程用)通信 ...

  8. [Repost]Events and Signals in PyQt4

    Reference:http://zetcode.com/gui/pyqt4/eventsandsignals/ Events and Signals in PyQt4 In this part of ...

  9. javascript MVC(每天有学习一点篇)

    “写的不是代码,是你的思维”,经常觉得自己写的代码“皮粗肉糙”的,看到那些要么精致小巧,要么优雅大方,要么光明磊落的代码时,常常会黯然神伤外加垂涎欲滴. why?(为什么我的代码不能如此..) whe ...

随机推荐

  1. 【BZOJ】2653: middle

    2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2381  Solved: 1340[Submit][Status][Disc ...

  2. bzoj 1086 树分块

    将树分成一些块,做法见vfleaking博客. /************************************************************** Problem: 108 ...

  3. python开发_pprint()

    python中的pprint.pprint(),类似于print() 下面是我做的demo: #python pprint '''python API中提供的Sample''' import json ...

  4. 51NOD 1179 最大的最大公约数 筛法

    1179 最大的最大公约数 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1179 Description ...

  5. linux基础命令学习(二)文件和目录操作

    1.变换当前目录(change directory)     cd /home 进入 '/ home' 目录'  (change directory)   cd .. 返回上一级目录    cd .. ...

  6. TortoiseSVN里锁lock 的使用方法

    刚才试验了一下,终于搞明白了TortoiseSVN里锁lock 的使用方法. 简单的说,如果压根没有锁lock,那么每个人都拥有一个本地copy,每个人都能自由地对本地copy编辑edit并提交com ...

  7. CentOS 6.9/7通过yum安装指定版本的Redis

    一.安装 // 安装依赖 # wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && ...

  8. apache 与 iis共存

    先装上了apache ,再装iis会出现错误.所以先关闭apache,再进行iis安装.iis安装完后修改iis80端口为8080端口, 同时使用命令 cd C:\Inetpub\AdminScrip ...

  9. Simple Addition Permits Voltage Control Of DC-DC Converter's Output

    http://electronicdesign.com/power/simple-addition-permits-voltage-control-dc-dc-converters-output In ...

  10. Spring SimpleJdbcTemplate查询示例

    这里有几个例子来说明如何使用SimpleJdbcTemplate query()方法来查询或从数据库中提取数据.在 JdbcTemplate query() 方法,需要手动转换返回的结果转换为一个目标 ...