Py designer 小程序实现实例

时间:2023-03-08 19:07:11
Py designer 小程序实现实例

前提

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)。

Py designer 小程序实现实例

图1 designer主界面

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

Py designer 小程序实现实例

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

Py designer 小程序实现实例

图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, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class 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 sys
from 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

Py designer 小程序实现实例