PyQt4学习记录之事件和信号
2014-01-17 16:29
519 查看
事件是任何 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测试代码:
ui_test.py UI生成代码:
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))
相关文章推荐
- PyQt5学习记录(4)---事件和信号
- PyQt5学习记录(7)---监听鼠标拖拽事件实现Mac上百度云盘拖拽效果
- PyQt4学习笔记2:事件和信号
- Pyqt4学习笔记-事件和信号
- Python pyQt4/PyQt5 学习笔记4(事件和信号)
- PyQt4 学习笔记4之事件与信号
- iOS如何绑定控件事件(学习记录)
- 2017.7.14 学习记录 JavaScript 输入框的事件及获取属性值
- 学习记录:touch事件的坐标获取
- PyQT信号槽_学习笔记 (Signals/Slots)
- PyQT信号槽_学习笔记 (Signals/Slots)
- Qt学习记录--02 Qt的信号槽机制介绍(含Qt5与Qt4的差异对比)
- PyQt5学习记录(2)---QMainWindow菜单栏、状态栏和工具栏
- GTK学习[一]:GTK中的信号(Signal)与事件
- PyQt 4.5+控件的信号槽事件定义方法
- c#事件 学习记录
- Android学习记录:MotionEvent,onTouch,OnTouchListener 事件机制等学习
- pyQt5-事件和信号
- PyQt 4.5+控件的信号槽事件定义方法
- PyQt5(4)——控件事件响应与信号槽