PyQt入门,一个简单的文本编辑器
使用QtDesigner创建UI界面
如图,给关闭按钮添加关闭操作/
form命名为:notepad
打开按钮命名为:button_open
保存按钮命名为:button_save
文件名保存为notepad.ui
2.使用pyuic编译ui文件为py文件 :
pyuic4 notepad.ui > notepad.py
编译后为打开文件notepad.py,代码为:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'C:\Users\Administrator\Desktop\testqt\edytor.ui' # # Created: Fri Aug 24 15:24:55 2012 # by: PyQt4 UI code generator 4.9.1 # # WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: _fromUtf8 = lambda s: s class Ui_notepad(object): def setupUi(self, notepad): notepad.setObjectName(_fromUtf8("notepad")) notepad.resize(519, 372) self.button_open = QtGui.QPushButton(notepad) self.button_open.setGeometry(QtCore.QRect(80, 30, 111, 23)) self.button_open.setObjectName(_fromUtf8("button_open")) self.pushButton_2 = QtGui.QPushButton(notepad) self.pushButton_2.setGeometry(QtCore.QRect(330, 30, 141, 23)) self.pushButton_2.setObjectName(_fromUtf8("pushButton_2")) self.editor_window = QtGui.QTextEdit(notepad) self.editor_window.setGeometry(QtCore.QRect(80, 80, 391, 211)) self.editor_window.setObjectName(_fromUtf8("editor_window")) self.button_save = QtGui.QPushButton(notepad) self.button_save.setGeometry(QtCore.QRect(210, 30, 101, 21)) self.button_save.setObjectName(_fromUtf8("button_save")) self.retranslateUi(notepad) QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), notepad.close) QtCore.QMetaObject.connectSlotsByName(notepad) def retranslateUi(self, notepad): notepad.setWindowTitle(QtGui.QApplication.translate("notepad", "Form", None, QtGui.QApplication.UnicodeUTF8)) self.button_open.setText(QtGui.QApplication.translate("notepad", "打开", None, QtGui.QApplication.UnicodeUTF8)) self.pushButton_2.setText(QtGui.QApplication.translate("notepad", "关闭", None, QtGui.QApplication.UnicodeUTF8)) self.button_save.setText(QtGui.QApplication.translate("notepad", "保存", None, QtGui.QApplication.UnicodeUTF8))
3. 写调用和UI的操作,建立test.py文件
test.py:
#--*-- coding:utf-8-- import sys from PyQt4 import QtCore, QtGui from edytor import Ui_notepad class StartQt4(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_notepad() self.ui.setupUi(self) QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"),self.file_dialog) QtCore.QObject.connect(self.ui.button_save,QtCore.SIGNAL("clicked()"),self.file_save) def file_save(self): #保存文件 from os.path import isfile if isfile(self.filename): file = open(self.filename,'w') file.write(self.ui.editor_window.toPlainText()) file.close() def file_dialog(self): #打开文件操作 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 = StartQt4() myapp.show() sys.exit(app.exec_())
4. 运行test.py 文件即可使用了.
作者:呆头龙
出处:http://www.cnblogs.com/waniu/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-呆头龙。
欢迎园友讨论下自己的见解,及推荐更好资料。
本文如对读者有帮助,还请多帮
下此文。
谢谢!!! (
)