事件是任何 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. DNA比对

    [编程题](满分27分) 脱氧核糖核酸即常说的DNA,是一类带有遗传信息的生物大分子.它由4种主要的脱氧核苷酸(dAMP.dGMP.dCMT和dTMP)通过磷酸二酯键连接而成.这4种核苷酸可以分别记为 ...

  2. Miaomiao's Geometry

    HDU 4932  Bestcoder Problem Description There are N point on X-axis . Miaomiao would like to cover t ...

  3. How to update FVDI Commander driver to latest V2015.6.2

    As FVDI Commander products are upgraded to new versions, I often receive emails from customers askin ...

  4. cocos2d-x使用ant批量打包

    当项目需要在多渠道上线时,要打很多的渠道包,少则几十个,多种几百个.它们的区别一般只是渠道id或部分配置信息不同,这些信息均可写在配置文件中. 例如常见的渠道id不同,一般定义在AndroidMani ...

  5. nodejs的mysql模块学习(三)数据库连接配置选项详解

    连接选项 当在创建数据连接的时候 第一种大多数人用的方法 let mysql = require('mysql'); let connection = mysql.createConnection({ ...

  6. CF Amr and Music (贪心)

    Amr and Music time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. 通用函数get和set

    这两个函数在画图时很常用,所有对象都有属性来定义它们的特征,正是通过设定这些属性来修正图形显示的方式.尽管许多属性所有的对象都有,但与每一种对象类型(比如坐标轴,线,曲面)相关的属性列表都是独一无二的 ...

  8. python中那些双下划线开头得函数和变量--转载

    Python中下划线---完全解读     Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用'from module import *'导入 __xxx__ 系统定义名字 __x ...

  9. VS操作Sqlite数据库

    如果你下载的Sqlite是不带bundle的版本,那还要把这个SQLite.Interop.dll复制到debug执行文件夹下才能执行.

  10. MyBatis(3.2.3) - Configuring MyBatis using XML, Settings

    The default MyBatis global settings, which can be overridden to better suit application-specific nee ...