博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Py designer 小程序实现实例
阅读量:4687 次
发布时间:2019-06-09

本文共 3397 字,大约阅读时间需要 11 分钟。

前提

1、已安装Python(环境Win7+Python 2.7.12 )

2、已安装插件pyqt4(插件获取路径:https://pan.baidu.com/s/1i50wAOH)

步骤

1、打开py designer制作界面(C:\Python27\Lib\site-packages\PyQt4\designer.exe)。

图1 designer主界面

2、选择Widget窗体形式,点击创建,设置3个Push Button和1个textEdit,并修改控件文本名称和对象名称。

图2 改变文本名称和对象名称的方法

 

图3 设计结果图

3、保存,设置文件名为Example_edit.ui(C:\Python27\Lib\site-packages\PyQt4\Example_edit.ui)

4、使用pyuic编译ui文件为py文件。

c:\Python27\Lib\site-packages\PyQt4>pyuic4 Example_edit.ui > Example_edit.py

c:\Python27\Lib\site-packages\PyQt4>

打开Example_edit.py文件,具体内容如下:

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'Example_edit.ui'## Created: Mon May 01 07:32:43 2017#      by: PyQt4 UI code generator 4.8.5## WARNING! All changes made in this file will be lost!from PyQt4 import QtCore, QtGuitry:    _fromUtf8 = QtCore.QString.fromUtf8except AttributeError:    _fromUtf8 = lambda s: sclass Ui_Form(object):    def setupUi(self, Form):        Form.setObjectName(_fromUtf8("Form"))        Form.resize(617, 401)        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))        self.open = QtGui.QPushButton(Form)        self.open.setGeometry(QtCore.QRect(130, 70, 91, 31))        self.open.setText(QtGui.QApplication.translate("Form", "打开", None, QtGui.QApplication.UnicodeUTF8))        self.open.setObjectName(_fromUtf8("open"))        self.save = QtGui.QPushButton(Form)        self.save.setGeometry(QtCore.QRect(300, 70, 81, 31))        self.save.setText(QtGui.QApplication.translate("Form", "保存", None, QtGui.QApplication.UnicodeUTF8))        self.save.setObjectName(_fromUtf8("save"))        self.close = QtGui.QPushButton(Form)        self.close.setGeometry(QtCore.QRect(440, 70, 81, 31))        self.close.setText(QtGui.QApplication.translate("Form", "关闭", None, QtGui.QApplication.UnicodeUTF8))        self.close.setObjectName(_fromUtf8("close"))        self.textEdit = QtGui.QTextEdit(Form)        self.textEdit.setGeometry(QtCore.QRect(90, 130, 441, 241))        self.textEdit.setObjectName(_fromUtf8("textEdit"))        self.retranslateUi(Form)        QtCore.QMetaObject.connectSlotsByName(Form)    def retranslateUi(self, Form):        pass

5、建立test.py文件,编写调用和UI的操作。

#--*-- coding:utf-8--import sysfrom PyQt4 import QtCore, QtGui from Example_edit import Ui_Form class StartQt4(QtGui.QMainWindow):     def __init__(self, parent=None):         QtGui.QWidget.__init__(self, parent)         self.ui = Ui_Form()         self.ui.setupUi(self)         QtCore.QObject.connect(self.ui.open,QtCore.SIGNAL("clicked()"),self.file_dialog)         QtCore.QObject.connect(self.ui.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.textEdit.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.textEdit.setText(text) if __name__ == "__main__":     app = QtGui.QApplication(sys.argv)     myapp = StartQt4()     myapp.show()     sys.exit(app.exec_())

6、测试结果,结束

c:\Python27\Lib\site-packages\PyQt4>python test.py

 

转载于:https://www.cnblogs.com/linyfeng/p/6790959.html

你可能感兴趣的文章
crontab调用python脚本新思路
查看>>
df和du显示的磁盘空间使用情况不一致的原因及处理(文件删除后磁盘空间不释放)...
查看>>
进程与线程的关系与区别
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
Pandas基础(十一)时间序列
查看>>
arrow:让Python的日期与时间变的更好
查看>>
MySQL命令行参数
查看>>
MFC中 用Static控件做超链接(可以实现变手形、下划线、字体变色等功能)
查看>>
20144303 《Java程序设计》第五周学习总结
查看>>
多线程(第三天)
查看>>