如何使用python在没有按键事件的情况下在内部更新/重绘GTK小部件(GTKLabel)?

时间:2011-12-05 06:14:22

标签: python gtk pygtk

我有一些代码试图更新GTK Label元素。我包括两个文件:ui文件和py文件。

UI文件:

<glade-interface>
  <widget class="GtkWindow" id="ApplicationFrame">
    <property name="width_request">320</property>
    <property name="height_request">240</property>
    <property name="visible">True</property>
    <property name="events">GDK_KEY_PRESS_MASK</property>
    <property name="title" translatable="yes">Simple</property>
    <property name="resizable">False</property>
    <property name="window_position">center-always</property>
    <property name="default_width">320</property>
    <property name="default_height">240</property>
    <property name="decorated">False</property>
    <property name="gravity">center</property>
    <child>
      <widget class="GtkFixed" id="layout">
        <property name="width_request">320</property>
        <property name="height_request">240</property>
        <property name="visible">True</property>
        <child>
          <widget class="GtkLabel" id="l1">
            <property name="width_request">320</property>
            <property name="height_request">40</property>
            <property name="visible">True</property>
            <property name="xalign">0</property>
            <property name="label" translatable="yes">l1</property>
          </widget>
          <packing>
            <property name="y">43</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>

Python文件

import os
from time import sleep as wait
import gtk as gtk
import gtk.glade as glade
import gobject

class Application(object):
    def __init__ (self):
        self.glade = glade.XML("simple.ui")
        self.setup_ui()

    def setup_ui (self):
        self.window = self.glade.get_widget("ApplicationFrame")
        self.l1 = self.glade.get_widget("l1")
        self.names = {'l1' : self.l1}
        self.all = [self.l1]
        gobject.timeout_add(1000,self.display_me)
        gobject.timeout_add(100,self.do_something_that_takes_a_while)
        self.window.add_events(gtk.gdk.KEY_PRESS_MASK)
        self.window.connect("key-press-event", self.handler)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.show()

    def get_signal (self,widget,event):
        keyname = gtk.gdk.keyval_name(event.keyval)
        ctrl = event.state & gtk.gdk.CONTROL_MASK
        alt = event.state & gtk.gdk.MOD1_MASK
        shift = event.state & gtk.gdk.SHIFT_MASK
        name = []
        if ctrl and keyname not in ["Control_L","Control_R"]: 
            name.append("CTRL")
        if alt and keyname not in ["Alt_L","Alt_R"]:
            name.append("ALT")
        if shift and keyname not in ["Shift_L","Shift_R"]:
            name.append("SHIFT")
        name.append(keyname)
        name = "+".join(name)
        return name

    def handler (self,widget,event):
        name = self.get_signal(widget,event)
        if name.lower() in ['ctrl+x','ctrl+c','alt+q']:
            self.destroy()

    def main(self):
        gtk.main()

    def delete_event (self,widget=None,event=None,data=None):
        return False

    def destroy (self,widget=None,data=None):
        gtk.main_quit()

    def get (self,item):
        if isinstance(item, str): 
            if item in self.names:
                item = self.names[item]
        retval = None
        if hasattr(item,"text"):
            retval = item.text()
        elif hasattr(item,"get_label"):
            retval = item.get_label()
        return retval

    def set (self,item,text='',font=None):
        print 'Setting...'
        if isinstance(item, str): 
            if item in self.names:
                item = self.names[item]
        retval = None
        if font == None and hasattr(self,"page") and hasattr(self.page,"NORMAL_FONT"): 
                font = self.page.NORMAL_FONT
        if hasattr(item,"setText"):
            retval = item.setText(text)
        elif font == None:
            if hasattr(item,'set_text'):
                retval = item.set_text(text)
        elif hasattr(item,"set_label"):
            retval = item.set_label(text)
            if hasattr(item,"modify_font") and font != None:
                item.modify_font(font)
        item.queue_draw()
        self.try_to_update(item)
        print 'done'
        return retval

    def try_to_update (self,item):
        """
        do something here to update the visual screen????
        """
        print str(self)

    def do_something_that_takes_a_while (self):
        timeout = 15
        while timeout != 0:
            self.set('l1','%s' % timeout)
            wait(1)
            timeout -= 1
            print timeout
        return 1

    def clean (self):
        if item != None:
            self.set(item,empty_text)
        else:
            if hasattr(self,'all'):
                for item in self.all:
                    self.set(item)

    def display_me (self):
        print str(self)
        return True

    def __str__ (self):
        space = 25
        value = '%'+str(space)+'s'
        lines = ['\n','-'*79]
        if hasattr(self,'all'):
            line = []
            for obj in self.all:
                obj_value = self.get(obj)
                line.append(value % obj_value if obj_value != None else '')
                #line.append(value % ' ')
            lines.append(''.join(line))
        lines.append('-'*79)
        return '\n'.join(lines)


if __name__ == "__main__":
    from time import sleep as wait
    SEC = 1
    app = Application()
    app.main()

这应该很简单,但我完全不知道我做错了什么。元素l1未正确更新。我认为try_to_update中的代码是我需要更新UI的地方,但是我不知道需要调用哪个函数。有人可以帮忙吗?

提前致谢!

2 个答案:

答案 0 :(得分:9)

您需要使用Widget的queue_draw功能:

  

queue_draw_area()方法通过调用窗口小部件窗口及其所有子窗口上的 gtk.gdk.Window.invalidate_rect() 方法,使窗口小部件(...)的矩形区域无效。 / em>的

但是,正如您已经在set方法中所做的那样,我的猜测是

if hasattr(item,'show')

检查可防止项目更新。另一项检查是调用整个窗口上的queue_draw函数,看看是否更新了这些内容。

或者,强制排队的事件按如下方式处理:

while gtk.events_pending():
    gtk.main_iteration_do(True)

答案 1 :(得分:2)

代码中发生的一件事是,在执行do_something_that_takes_a_while时,不会处理任何事件。解决这个问题的一种方法是在该方法中的某个点强制进行事件处理:

--- code.py 2011-12-05 10:32:53.000000000 +0100
+++ code.py.bak 2011-12-05 10:27:22.000000000 +0100
@@ -95,8 +95,6 @@
         timeout = 15
         while timeout != 0:
             self.set('l1','%s' % timeout)
-            while gtk.events_pending():
-                gtk.main_iteration()
             wait(1)
             timeout -= 1
             print timeout