前提

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, 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 小程序实现实例的更多相关文章

  1. 微信小程序web-view实例

    微信小程序web-view实例 index.js //index.js //获取应用实例 const app = getApp() Page({ /** * 页面的初始数据 */ data: { }, ...

  2. 微信小程序入门实例之记事本

    主要实现思想都在代码的注释中,项目源码见github 首先上项目目录 app.js文件代码如下: //app.js App({ onLaunch: function() { //调用API从本地缓存中 ...

  3. 微信小程序-weui实例代码提取

    搜索框 对应代码如下: wxss文件 <view class="page"> <view class="page__hd"> <v ...

  4. 微信小程序开发入门

    微信小程序 首先说下结构吧,看看小程序到底长什么样子 这是一个微信提供的自己的开发工具,相当于xcode吧,由此也可以看出腾讯的野心并不小啊,左边的就是编辑调试什么的,往右就是一个模拟器,你可以选择i ...

  5. 微信小程序教学第二章:小程序中级实战教程之预备篇 - 项目结构设计 |基于最新版1.0开发者工具

    iKcamp官网:http://www.ikcamp.com 访问官网更快阅读全部免费分享课程:<iKcamp出品|全网最新|微信小程序|基于最新版1.0开发者工具之初中级培训教程分享>. ...

  6. 微信小程序个人心得

    尊重原创:http://blog.csdn.net/qq_28832135/article/details/52796048 昨天看了一下微信小程序官方文档,总结一下自己学习的个人心得. 首先从官方文 ...

  7. 用Vue.js开发微信小程序:开源框架mpvue解析

    前言 mpvue 是一款使用 Vue.js 开发微信小程序的前端框架.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为 H5 和小程序提供了代码复用的能力.如果想将 H5 项目改造为小程 ...

  8. 微信小程序心得

    首先从官方文档给的框架说起,微信小程序官方文档给出了app.js, app.json, app.wxss. 先从这三个文件说起. - app.js 这个文件是整个小程序的入口文件,开发者的逻辑代码在这 ...

  9. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

随机推荐

  1. 第11章 拾遗4:IPv6(2)_给计算机配置IPv6地址

    4. 给计算机配置IPv6地址 4.1 无状态自动配置IPv6地址 (1)网络拓扑 ①无状态地址自动配置是指不需要DHCP服务器进行管理,由客户端向路由器发送前缀请求(RS)询问其所在网段.路由器收到 ...

  2. Visual Studio 20年

    这是一个暴露年龄的话题,如果一个程序员从第一个版本开始使用Visual Studio的话,现在应该是40多岁的中年大叔了.我的程序员生涯是从Visual basic 6.0 (vb6)开始的,一晃就过 ...

  3. tkinter简单使用

    第一个运行程序 # -*- coding: utf-8 -*- import tkinter as tk //引入 root = tk.Tk() // 实例化root T大写k小写 root.titl ...

  4. 用socket发送信息在浏览器上显示出来

    服务端代码: import socket def main(): sock=socket.socket() sock.bind(('localhost',8089)) sock.listen(5) w ...

  5. 图解RabbitMQ

    花点时间重新把rabbitMQ的整体流程粗略的梳理了一遍,便于理解.总体来说,消息产生者->queue->消息消费者可以图解如下: 文字解说:消息产生者 producter 和 consu ...

  6. 小数据池 id

    1. 小数据池, id() 小数据池针对的是: int, str, bool 在py文件中几乎所有的字符串都会缓存. id() 查看变量的内存地址 # id()函数可以帮我们查看一个变量的内存地址 # ...

  7. getpass模块 ---- optparse

    getpass模块,不回显 常用方法 import getpass password=getpass.getpass('input your password: ').strip() 其他用法 use ...

  8. Oracle 日志归档 自动清理

    exp emis/emis@orcl file=d:\backup\oracle\oracle%date:~0,10%.dmp owner=emis log=d:\backup\oracle\orac ...

  9. spring 之 lookup-method & replace-method

    初始化bean的堆栈: at org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy$Cglib ...

  10. hive随机采样

    hive> select * from account limit 10;OKaccount.accountname     account.accid   account.platid  ac ...