sinanjj 发表于 2010-8-22 23:46

GUI 编程Qt Designer With PyQt4

本帖最后由 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 =
    #~ console = ["hello.py"],
)

xmdongd8899 发表于 2011-4-13 17:27

顶!正在学习中

sinanjj 发表于 2011-4-30 14:21

本帖最后由 sinanjj 于 2011-4-30 14:22 编辑

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
def ip2long (ip):
      "将点分十进制 IP 地址转换成无符号的长整数"
      return struct.unpack("!I", socket.inet_aton(ip))
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))      # "将点分十进制 IP 地址转换成无符号的长整数"
      return "%08X" %(ip_int)
def port2hex (port):
      port_int = int(port)
      port_hex = "%08X" %(port_int)
      port_hex = port_hex
      port_hex = port_hex + port_hex
      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
    sum = 0
    for i in range(0, (len(temp))//2):
      sum += int (temp,16)
    sum = sum % 0x100
    sum = 0x100 - sum
    sum_str = "%2.2X" %sum
    return hex_line + sum_str[-2:] +'\n'

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
页: [1]
查看完整版本: GUI 编程Qt Designer With PyQt4