事件是任何 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. Spring 中jdbcTemplate 实现执行多条sql语句

    说一下Spring框架中使用jdbcTemplate实现多条sql语句的执行: 很多情况下我们需要处理一件事情的时候需要对多个表执行多个sql语句,比如淘宝下单时,我们确认付款时要对自己银行账户的表里 ...

  2. 在openshift上使用django+postgresql

    openshift上用的是django 1.7,数据库选择的是postgresql 9.2 本地开发用的是sqlite3数据库,发布到openshift上后是没有数据的(本地的sqlite3数据库里的 ...

  3. 邮件工具类--EmailUtil

    /* Copyright Notice ===================================================* * This file contains propri ...

  4. markdownpad2 pro注册信息升级 破解版

    注册信息邮箱地址: Soar360@live.com 授权秘钥: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2 ...

  5. LeetCode 142

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  6. poj 3159 差分约束

    思路:班长的糖果要比snoopy的多.并且要用手写堆栈,且堆栈的大小要开到20000000. #include<iostream> #include<cstdio> #incl ...

  7. java按行读取txt并按行写入

    IO流想必大家都很熟悉了,本次实现的需求是按行读取文件内容并且按行写入,代码如下: try { String encoding="utf-8"; //设定自己需要的字符编码集 Fi ...

  8. CSS—换行

    关于文本溢出换行问题,先看下涉及到换行的相关属性,查看:http://www.w3school.com.cn 一.word-break 定义:word-break 属性规定自动换行的处理方法. 值 描 ...

  9. WCF配置文件详解 【转】

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  10. Git - Eclipse 提交工程至 GitHub

    1. 在 GitHub 新建一个工程 hello-world,repository 地址是 https://github.com/username/hello-world.git 2. 在 Eclip ...