我在更新自定义 GTK+ 小部件时遇到问题:VUWidget。Generator 类正在更新 Section 类的 level 属性,其子类具有 VUWidget 属性。Generator 类正确更新关卡属性的值。
import pygtk
pygtk.require("2.0")
import gtk, gtk.gdk
import cairo
#=========================================================================
class VUWidget(gtk.DrawingArea):
__gtype_name__ = 'VUWidget'
_BACKGROUND_RGB = (0., 0., 0.)
_LEVEL_GRADIENT_BOTTOM_ORGBA = (1, 0, 1, 0, 1)
_LEVEL_GRADIENT_TOP_ORGBA = (0, 1, 0, 0, 1)
#_____________________________________________________________________
def __init__(self, section):
gtk.DrawingArea.__init__(self)
self.section = section
self.connect("configure_event", self.on_configure_event)
self.connect("expose-event", self.OnDraw)
self.section.connect("changed-value", self.ValueChanged)
self.set_size_request(30,100)
self.realize()
self.show()
#_____________________________________________________________________
def ValueChanged(self, widget, level):
#print ("Callback %f" % self.section.GetLevel())
rect = self.get_allocation()
self.window.invalidate_rect(rect, False)
return False
#_____________________________________________________________________
def GenerateBackground(self):
rect = self.get_allocation()
ctx = cairo.Context(self.source)
ctx.set_line_width(2)
ctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
pat = cairo.LinearGradient(0.0, 0.0, 0, rect.height)
pat.add_color_stop_rgba(*self._LEVEL_GRADIENT_BOTTOM_ORGBA)
pat.add_color_stop_rgba(*self._LEVEL_GRADIENT_TOP_ORGBA)
ctx.rectangle(0, 0, rect.width, rect.height)
ctx.set_source(pat)
ctx.fill()
#_____________________________________________________________________
def on_configure_event(self, widget, event):
self.source = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.allocation.width, self.allocation.height)
self.GenerateBackground()
return self.OnDraw(widget, event)
#_____________________________________________________________________
def OnDraw(self, widget, event):
ctx = self.window.cairo_create()
ctx.save()
rect = self.get_allocation()
ctx.rectangle(0, 0, rect.width, rect.height)
ctx.set_source_rgb(*self._BACKGROUND_RGB)
ctx.fill()
ctx.rectangle(0, rect.height * (1. - self.section.GetLevel()), rect.width, rect.height)
ctx.clip()
ctx.set_source_surface(self.source, 0, 0)
ctx.paint()
ctx.restore()
return False
#_____________________________________________________________________
def Destroy(self):
del self.source
self.destroy()
#_____________________________________________________________________
#=========================================================================
信号在类 Section 中实现并正确发出
__gsignals__ = {
'changed-value' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_FLOAT,))
}
问候CK