PyQt 4学習記録のイベントと信号
16843 ワード
イベントは、GUIプログラムの重要な部分です.すべてのPython GUIアプリケーションはイベント駆動です.1つのアプリケーションは、そのライフサイクルで発生する異なるイベントタイプに反応する.イベントは、主にアプリケーションのユーザーによって生成されます.しかし、ネットワーク通信、ウィンドウの管理者、タイマーなど、他の方法で生成することもできる.
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))