Skip to main content
 首页 » 编程设计

python之在布局 : problem with subclassing gtk. 可绘制对象上绘制我自己的对象

2025年05月04日8youxin

我想在布局上绘制我自己的对象,所以我尝试子类化 gdk.drawable,

class link(gtk.gdk.Drawable): 
  def __init__(self,comp1,comp2,layout): 
      super(link, self).__init__() 
      self.x1=comp1.xpos 
      self.y1=comp1.ypos 
      self.x2=comp2.xpos 
      self.y2=comp2.ypos 
      self.layout=layout 

错误:

cannot create instance of abstract (non-instantiable) type `GdkDrawable'

我可以在链接对象的方法 drawlink() 中使用 layout.bin_window.draw_line() 子类化 drawable 来完成它,但我无法为每个对象创建自定义图形上下文 gdk.gc,而且我有使用 layout.get_style() 这对我所有的链接都是一样的!

  def drawlink(self): 
    gc = self.layout.get_style().fg_gc[gtk.STATE_NORMAL] 
    gc.line_style=gtk.gdk.LINE_ON_OFF_DASH 
    gc.line_width=6 
    self.layout.bin_window.draw_line(gc,self.x1, self.y1, self.x2, self.y2)  

这就是我想要子类化可绘制对象的原因。如果我可以使用自定义 gc 而无需子类化 drawable or(window, pixmap) 那就太好了。

谢谢

请您参考如下方法:

any alternative?

如果我对你的理解正确,你想要做的不是子类化 gtk.gdk.Drawable 而是填充一个 gtk.DrawingArea小部件 与您自己的内容。该小部件的描述是:

The gtk.DrawingArea widget is used for creating custom user interface elements. It's essentially a blank widget containing a gtk.gdk.Window that you can draw on.