跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Python 开发
  4. PyGTK初体验

PyGTK初体验

已定时 已固定 已锁定 已移动 Python 开发
5 帖子 3 发布者 11.9k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • M 离线
    M 离线
    mihi
    写于 最后由 编辑
    #1

    GUI(图形用户接口)在程序开发中起举足轻重的作用。从某种角度来说MVC也是一个GUI类库,再加上GTK和QT可以并称三大GUI类库。期中前者主要用于windows平台,后两者用在Linux平台较多。GTK是Gnome的底层类库,而QT是KDE的底层类库。相对而言QT比GTK名气更大,但是GTK比QT更加开放。因此本人Python下第一次用户界面程序开发就使用GTK下的PyGTK。


    PyGTK顾名思义是Python下对GTK的封装。GTK下也有窗体Form和控件的概念。只是控件被称之为widget。窗体和widget都是GObejct是子类。Gobject是GTK的一个基类,在GObject中可以自定义事件和属性。(内容相对复杂就不展开了)

    对应于简单的GTK窗口开发的步骤大概可以分为三步。
    1widget设计布局。并加载widget。
    2事件方法关联。并实现方法。
    3显示窗口,注意GTK主循环.

    首先是widget的布局。GTK除了动态控制控件位置之外,还支持一种Glade布局。本质上是把控件的位置及其其他属性信息写在XML文件中。(这里称为glade,同时glade也是一个可视化配置软件。用来设计时给widget布局,但是不好用。)然后在类初始化的时候,把glade文件加载,并找到(通过Id名称)我们需要的widget。然后再把window显示出来,就实现窗口布局。

    第二个关键问题是事件方法的关联。在VB和delphi中我们只要双击GUI设计界面中的按钮,一般就会自动实现关联。GTK中做的更加底层,也就更加复杂。首先引入singal信号,通过信号把事件和处理联系起来。抽取Glade文件内容为代码1

    代码1

    <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;widget class=&quot;GtkButton&quot; id=&quot;btnWrite&quot;&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;width_request&quot;&gt;60&lt;/property&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;height_request&quot;&gt;30&lt;/property&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;visible&quot;&gt;True&lt;/property&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;can_focus&quot;&gt;True&lt;/property&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;receives_default&quot;&gt;True&lt;/property&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;response_id&quot;&gt;0&lt;/property&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;signal name=&quot;clicked&quot; handler=&quot;OnWrite&quot;/&gt;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/widget&gt;<br />
    


    <signal name="clicked" handler="OnWrite"/>就是所谓的信号,但是注意 handler 中的OnWrite还不是最终的方法。

    还需要第二步在py文件中__init(self)__方法中使用 res.signal_autoconnect(signal)其中signal是一个字典表示handler和方法的对应关系如:signal = {'OnRead':self.OnRead,'OnWrite':self.OnWrite}

    第三主循环的引入,对于窗口程序而言,需要引入主循环,用来等待事件的触发。在gtk中为

    gtk.main()

    了解了以上三部分内容基本上就可以进行GTK基本的页面开发了,见代码2:。

    代码2:

    <br />class CardIOForm():<br /><br />&nbsp; &nbsp; def __init__(self):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; res = gtk.glade.XML(&#039;cardio.glade&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; self.win1=res.get_widget(&#039;cardioform&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; self.entryCode=res.get_widget(&#039;entryCode&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; signal = {&#039;OnRead&#039;:self.OnRead,&#039;OnWrite&#039;:self.OnWrite,&#039;OnClose&#039;:self.OnClose,&#039;OnWritePwd&#039;:self.OnWritePwd,}<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; res.signal_autoconnect(signal)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; def OnRead(self,widget):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;读第2扇区&quot;<br /><br />&nbsp; &nbsp; &nbsp;  pass<br /><br />&nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; <br /><br />&nbsp; &nbsp; def OnWrite(self,widget):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;写入编码&quot;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; pass<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; def OnWritePwd(self,widget):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;先写入密码&quot;&nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; &nbsp;  pass<br /><br />&nbsp; &nbsp;  <br /><br />&nbsp; &nbsp; def show(self):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; self.win1.show()<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; def OnClose(self,widget):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; gtk.main_quit()<br /><br /><br /><br />if __name__ == &#039;__main__&#039;:<br /><br />&nbsp; &nbsp; window = CardIOForm()<br /><br /> window.show()<br /><br />&nbsp; &nbsp; gtk.main()<br />
    




    参考文献:

    PyGTK 2.0 Reference Manual http://developer.gnome.org/pygtk/stable/ br />PyGTK 2.0 Tutorial(可下载pdf)

    1 条回复 最后回复
    0
    • N 离线
      N 离线
      NewZN
      写于 最后由 编辑
      #2

      老梅牛!
      本文提供了一个OpenERP和外部设备接口的范例!

      1 条回复 最后回复
      0
      • M 离线
        M 离线
        mihi
        写于 最后由 编辑
        #3

        对应Mifire One(就是最标准的射频IC卡)的读写部分,我删去了。因为这部分是个性的,没有普遍意义,如果有特殊需要的话,我可以提供完整的代码,进行交流。

        1 条回复 最后回复
        0
        • mrshellyM 离线
          mrshellyM 离线
          mrshelly
          写于 最后由 编辑
          #4

          这个必须的....顶....

          1 条回复 最后回复
          0

          • 登录

          • 没有帐号? 注册

          • 登录或注册以进行搜索。
          • 第一个帖子
            最后一个帖子
          0
          • 版块
          • 标签
          • 热门
          • 用户
          • 群组