打印
[嵌入式linux]

GUI 编程Qt Designer With PyQt4

[复制链接]
5281|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sinanjj|  楼主 | 2010-8-22 23:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 sinanjj 于 2010-11-26 10:21 编辑

python + qt designer的一个简单例子

使用的组件版本如下:
  * Python version 3.1.1
  * PyQt4
  * Qt designer 4.6.0

安装(windows):
直接下载activepython3.1
和pyqt4 安装.
http://www.riverbankcomputing.co.uk/software/pyqt/download

用qtdesigner设计GUI.
1, 打开qt designer (linux下直接designer &或者找菜单, 现在菜单里都有. window下 "开始"-->"所有程序"-->"pyqt OOXX" --->"designer" )
2, 建立一个空组件(widget, 怎么理解您看着办, 结构上是所有ui类的根类)

4. 现在给这个窗口增加点控件
5. 在左边的组件里, 找到push button 组件, 拖拽到窗口里(注意是拖拽, 不要松鼠标)
6. 类似的, 拖入 lineEdit 和textEdit控件.
双击控件, 改变他们的显示.
. 最后弄成这个样子.


相关帖子

沙发
sinanjj|  楼主 | 2010-8-23 00:37 | 只看该作者
本帖最后由 sinanjj 于 2010-11-26 10:28 编辑

8. 右上角的是对象一览窗口. 你可以双击改变ui对象的名字.(这个名字就是程序中的对象名字, 程序中的索引)


9. 最后注意, 我们应该关闭  按钮的"autoDefault" 这个属性 (默认就是关的....)


10, 将界面保存为文件. 比如: gui.ui.


将这个xml格式的文件转变为pyqt界面代码
这个命令将gui.ui文件转变为gui.py文件

> pyuic4 gui.ui -o gui.py



使用特权

评论回复
板凳
sinanjj|  楼主 | 2010-11-25 23:23 | 只看该作者
本帖最后由 sinanjj 于 2010-11-26 10:45 编辑

写个封包类将这个界面跑起来.

创建一个新文件名为 run.py

内容:

#!/usr/bin/python -d
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtCore, QtGui
from gui import Ui_Form    # 导入我们创建的类

class MyForm(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.pushButton, QtCore.SIGNAL("clicked()"), self.ui.textEdit.clear)    # 注册事件 QtCore.QObject.connect(sender_widget, signal_received, action/methods )
        QtCore.QObject.connect(self.ui.lineEdit, QtCore.SIGNAL("returnPressed()"), self.add_entry)
    def add_entry(self):
        self.ui.lineEdit.selectAll()
        self.ui.lineEdit.cut()
        self.ui.textEdit.append("")
        self.ui.textEdit.paste()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())


看注释, 剩下运行 python run.py

使用特权

评论回复
地板
sinanjj|  楼主 | 2010-11-29 10:46 | 只看该作者
本帖最后由 sinanjj 于 2011-4-30 14:15 编辑

summary


                                python/pyqt4/QtDesigner helloworld
                        1, GUI design
create a Main Window, drag-and-drop the widgets(TextEdit, LineEdit, PushButton), layout them.
save to helloworld.ui file.
                        2, *.ui > *.py
$ pyuic4 helloworld.ui > helloworld.py
                        3, Writing A Wrapper
run.py
#!/usr/bin/python -d
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from helloworld import Ui_MainWindow
class MyForm(QtGui.QMainWindow):
        def __init__(self, parent=None):
                QtGui.QWidget.__init__(self, parent)
                self.ui = Ui_MainWindow()
                self.ui.setupUi(self)
                QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.ui.textEdit.clear)
                QtCore.QObject.connect(self.ui.lineEdit, QtCore.SIGNAL("returnPressed()"), self.add_entry)
        def add_entry(self):
                self.ui.lineEdit.selectAll()
                self.ui.lineEdit.cut()
                self.ui.textEdit.append("")
                self.ui.textEdit.paste()
if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        myapp = MyForm()
        myapp.show()
        sys.exit(app.exec_())



setup.py:

from distutils.core import setup
import py2exe
import sys

# If run without args, build executables, in quiet mode.
if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("--includes")
    sys.argv.append("sip")

class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the versioninfo resources
        self.version = "0.5.0"
        self.company_name = "No Company"
        self.copyright = "no copyright"
        self.name = "py2exe sample files"

test = Target(
    # used for the versioninfo resource
    description = "A sample GUI app",

    # what to build
    script = "run.py",
    #~ other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog="test_wx"))],
    #icon_resources = [(1, "editra.ico")]
    #~ dest_base = "test_wx"
)
setup(
    # The first three parameters are not required, if at least a
    # 'version' is given, then a versioninfo resource is built from
    # them and added to the executables.
    #~ version = "0.5.0",
    #~ description = "py2exe sample script",
    #~ name = "py2exe samples",

    # targets to build
    windows = [test]
    #~ console = ["hello.py"],
)

使用特权

评论回复
5
xmdongd8899| | 2011-4-13 17:27 | 只看该作者
顶!正在学习中

使用特权

评论回复
6
sinanjj|  楼主 | 2011-4-30 14:21 | 只看该作者
本帖最后由 sinanjj 于 2011-4-30 14:22 编辑

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
def ip2long (ip):
        "将点分十进制 IP 地址转换成无符号的长整数"
        return struct.unpack("!I", socket.inet_aton(ip))[0]
def long2ip (lint):
        "将无符号长整形转换为点分十进制 IP 地址形式"
        return socket.inet_ntoa(struct.pack("!I", lint))
def ip2hex (ip):
        "将点分十进制 IP 地址转换成hex格式"
        ip_int = struct.unpack("I", socket.inet_aton(ip))[0]        # "将点分十进制 IP 地址转换成无符号的长整数"
        return "%08X" %(ip_int)
def port2hex (port):
        port_int = int(port)
        port_hex = "%08X" %(port_int)
        port_hex = port_hex[4:8]
        port_hex = port_hex[2:4] + port_hex[0:2]
        return port_hex

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


import os
import time
import random
import string
def toHex(s):    #convert string to hex
    lst = []
    for ch in s:
        hv = hex(ord(ch)).replace('0x', '')
        if len(hv) == 1:
            hv = '0'+hv
        lst.append(hv)
    return reduce(lambda x,y:x+y, lst)
# node_id = string.join(random.sample(['0','1','2','3','4','5','6','7','8','9'], 4)).replace(" ","")
def hex_checksum_update (hex_line):
    temp = hex_line[1:-4]
    sum = 0
    for i in range(0, (len(temp))//2):
        sum += int (temp[2*i:2*i+2],16)
    sum = sum % 0x100
    sum = 0x100 - sum
    sum_str = "%2.2X" %sum
    return hex_line[0:-3] + sum_str[-2:] +'\n'

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:In God We Trust 独立的个人,体赖科学技术工具提供针对个人的产品与服务,是通向幸福的唯一道路 工程师,设计师等可以个人创业的群体,将逐步瓦解官僚体制公司,成为中国中产。(重复劳动,工厂等,将逐步机械化) seacer.co

456

主题

6300

帖子

25

粉丝