更新自定义GTK+widget:VUWidget时出现问题。 生成器类正在更新类节的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()
#_____________________________________________________________________
#=========================================================================
该信号在类部分中实现并正确发出
^{pr2}$问候 Ck公司
这里有一种方法,我将其子类化并提供一个自定义的draw方法。如果状态有任何变化,我调用invalidate,强制小部件重新绘制暴露事件。如果需要不同的上下文,可以在on_expose_事件中提供 方法。在
所以基本上,你所有的画都只在一个地方。如果小部件应该显示不同的内容,请设置新状态并重新呈现。易于维护。在
对评论的补充回复:
在Gtk.窗口以及Gdk窗口在概念上是不同的。第一个是一个容器,它可以正好有一个其他小部件(其他容器或任何其他小部件)。第二个用于在显示上绘制内容并捕捉事件。这个gdk窗口在widget的“realize”事件中构造。在此之前,在PyGtk是没有的。以何为例,这是不可取的(甚至不知道是否可行)gtk.Window.Window. 自定义绘图应在gtk.图纸区域窗口. 这就是DrawingArea的目的。在
参考文献:
https://mail.gnome.org/archives/gtk-app-devel-list/1999-January/msg00138.html
GtkDrawingArea - how to make it drawable?
相关问题 更多 >
编程相关推荐