事件是任何 GUI程序中很重要的部分。所有 Python GUI 应用都是事件驱动的。一个应用对其生命期产生的不同的事件类型做出反应。事件是主要由应用的用户产生。但是,也可以通过其他方法产生,比如,网络通信,窗口的管理者,计时器。

PyQt 4.5 引入了新的 API 用于信号和槽。

这是旧式的 API 。

QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"), self.file_dialog)

新式的更接近 Python 的标准

button.clicked.connect(self.onClicked)

self.ui对应窗口,通过它我们可以访问窗口中的部件。因此,self.ui.button_open对应“打开”按钮。self.file_dialog是信号对应的函数,它是比较重要的部分。

test.py测试代码:

import sys
from PyQt4 import QtCore, QtGui
from ui_test import Ui_notepad class MyForm(QtGui.QMainWindow):def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_notepad()
self.ui.setupUi(self)
# here we connect signals with our slots
QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"), self.file_dialog)
def file_dialog(self):#self.ui.editor_window.setText('aaaaaaaaaa')
fd = QtGui.QFileDialog(self)
self.filename = fd.getOpenFileName()
from os.path import isfile
if isfile(self.filename):
text = open(self.filename).read()
self.ui.editor_window.setText(text) if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())

ui_test.py  UI生成代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created: Sat Jan 11 22:11:39 2014
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):return s try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):return QtGui.QApplication.translate(context, text, disambig) class Ui_notepad(object):def setupUi(self, notepad):
notepad.setObjectName(_fromUtf8("notepad"))
notepad.resize(692, 462)
self.button_open = QtGui.QPushButton(notepad)
self.button_open.setGeometry(QtCore.QRect(60, 10, 75, 23))
self.button_open.setObjectName(_fromUtf8("button_open"))
self.button_close = QtGui.QPushButton(notepad)
self.button_close.setGeometry(QtCore.QRect(290, 10, 75, 23))
self.button_close.setObjectName(_fromUtf8("button_close"))
self.editor_window = QtGui.QTextEdit(notepad)
self.editor_window.setGeometry(QtCore.QRect(20, 50, 651, 381))
self.editor_window.setObjectName(_fromUtf8("editor_window"))
self.button_save = QtGui.QPushButton(notepad)
self.button_save.setGeometry(QtCore.QRect(180, 10, 75, 23))
self.button_save.setObjectName(_fromUtf8("button_save")) self.retranslateUi(notepad)
QtCore.QObject.connect(self.button_close, QtCore.SIGNAL(_fromUtf8("clicked()")), notepad.close)
QtCore.QMetaObject.connectSlotsByName(notepad) def retranslateUi(self, notepad):
notepad.setWindowTitle(_translate("notepad", "Form", None))
self.button_open.setText(_translate("notepad", "打开", None))
self.button_close.setText(_translate("notepad", "关闭", None))
self.button_save.setText(_translate("notepad", "保存", None))

PyQt4学习记录之事件和信号的更多相关文章

  1. Python pyQt4/PyQt5 学习笔记4(事件和信号)

    信号 & 槽 import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import (QWidget,QLCDNumber,QS ...

  2. Qt学习记录--02 Qt的信号槽机制介绍(含Qt5与Qt4的差异对比)

    一 闲谈: 熟悉Window下编程的小伙伴们,对其消息机制并不陌生, 话说:一切皆消息.它可以很方便实现不同窗体之间的通信,然而MFC库将很多底层的消息都屏蔽了,尽管使用户更加方便.简易地处理消息,但 ...

  3. Echarts学习记录——如何给x轴文字标签添加事件

    Echarts学习记录——如何给x轴文字标签添加事件 关键属性 axisLabel下属性clickable:true 并给图表添加单击事件 根据返回值判断点击的是哪里 感觉自己的方法有点变扭,有更好办 ...

  4. UWP学习记录12-应用到应用的通信

    UWP学习记录12-应用到应用的通信 1.应用间通信 “共享”合约是用户可以在应用之间快速交换数据的一种方式. 例如,用户可能希望使用社交网络应用与其好友共享网页,或者将链接保存在笔记应用中以供日后参 ...

  5. UWP学习记录6-设计和UI之控件和模式3

    UWP学习记录6-设计和UI之控件和模式3 1.按钮 按钮,响应用户输入和引发 Click 事件的控件. 使用<Button>就能创建一个按钮控件了.按钮是 ContentControl, ...

  6. UWP学习记录4-设计和UI之控件和模式1

    UWP学习记录4-设计和UI之控件和模式1 1.控件和事件简介 在 UWP 应用开发中,控件是一种显示内容或支持交互的 UI 元素. 控件是用户界面的构建基块. 我们提供了超过 45 种控件供你使用, ...

  7. UWP学习记录2-设计和UI之布局

    UWP学习记录2-设计和UI之布局 1.导航基础知识 应用里,多个页面会有层次关系或者对等关系.这两种关系,通常在一个复杂应用里都会存在,而关系的选定依据: 对于对等导航,一般用选项卡(tabs)或者 ...

  8. [ZHUAN]Flask学习记录之Flask-SQLAlchemy

    From: http://www.cnblogs.com/agmcs/p/4445583.html 各种查询方式:http://www.360doc.com/content/12/0608/11/93 ...

  9. 【转】BLE 学习记录

    原文网址:http://m.blog.csdn.net/blog/chiooo/43985401 BLE 学习记录 ANROID BLE 开发,基于 bluetoothlegatt 分析 mBluet ...

随机推荐

  1. BootStrap2学习日记4---常用标签与样式

    <small>:常常和h1标签一起搭配使用 如<h1>标题<small>小标题</small></h1> <abbr>:abbr ...

  2. BootStrap2学习日记3--响应式布局实用类

    BootStrap2中常用的响应式布局类如: visible-phone     仅在 手机平台显示 visible-tablet      仅在 平板电脑显示 visible-desktop   仅 ...

  3. Football

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2882   Accepted: 1466 Descript ...

  4. jqcss选择器

    $("p").css("background-color","red"); $(this) 当前 HTML 元素$("p" ...

  5. Write a beautiful button

    .btn-warning { color: #fff; text-shadow: 0 -1px 0 rgba(0,0,0,0.25); background-color: #faa732; backg ...

  6. Java之MySql数据库链接

    一 下载MySql驱动包,下载途径很多,随便Google或度娘一下就有,我下载的是mysql-connector-java-5.1.26版本,下载后把它解压到指定路径 二 在Eclipse中新建项目T ...

  7. Linux下安装配置Node及memcached

    这篇主要是记录Linux下安装Node及memcached遇到的问题及安装配置过程,方便日后查阅 Node安装及配置 [root@hostname ~]tar zxvf node-v0.12.4.ta ...

  8. centos7重置root开机登录密码

    今天忘记了centos7 root登录的密码,本来要好好的做个图文的教程也好啊,但是忘记截图什么的,就不在重复的工作了, 参考了下面的两个链接重置了密码,结合使用效果更好哦,嘿嘿.. 下次要是再遇到这 ...

  9. LeetCode 326

    Power of Three Given an integer, write a function to determine if it is a power of three. Follow up: ...

  10. 媒体查询的应用以及在css3中的变革

    CSS一直都支持设置与媒体相关联的样式表.它们可以适应不同媒体类型的显示.例如,文档在屏幕显示时使用sans-serif字体,在打印时则使用serif字体.screen和print是两种预定义的媒体类 ...