无法在Gtk + 3中正确绘制

时间:2014-07-17 02:10:51

标签: gtk drawing pygtk gtk3 drawingarea

我可以在单个Gtk.DrawingArea中绘制,但是当我尝试为很多人绘制时,例如50, 我在绘图中遇到了一些错误

enter image description here

以下是您需要查看的代码:

def aggiorna(self, args=()):

        import random
        import time


        while True:
            for i in self.indirizzi_ip:
                self.r=random.randint(0,10)/10.0
                self.g=random.randint(0,10)/10.0
                self.b=random.randint(0,10)/10.0
                self.cpu_info[i]['drawing_area'].show() #the drawing happens here

            time.sleep(1)


    def __draw(self, widget, context): #connected to Gtk.DrawingArea.show()

        context.set_source_rgb(self.r, self.g, self.b) #random
        context.rectangle(0, 0, widget.get_allocated_width(), widget.get_allocated_height())
        context.fill()

1)为什么我在绘图时会出错?

2)为什么Gtk.DrawingArea(s)仅在我更新窗口时改变颜色(例如我从程序切换到Gtk.DrawingArea窗口)?

3)为什么我不为每个Gtk.DrawingArea获得随机颜色?

1 个答案:

答案 0 :(得分:1)

  1. 无法帮助您
  2. 因为它只会在Gtk.DrawingArea重绘自身时改变颜色(" draw" signal)
  3. r,g,b应该在里面"画"功能。你确实构造了r,g,b但是因为它在绘制函数之外,所以当重新绘制区域时它没有改变。
  4. 为什么睡觉?
  5. **编辑**

    示例代码:

      ....
      win = Gtk.Window ()
      box = Gtk.Box ()
      self.square_list = []
    
      for square in range (10):
        self.darea = Gtk.DrawingArea ()
        self.set_size_request (50,50)
        self.square_list.append (self.darea)
        box.add (self.darea)
    
        #for each darea connect to separate "draw" signal
        self.darea.connect ("draw", self,_draw)
    
      aggiorna_button = Gtk.Button ('Aggiorna!!!') #sorry i use button
      box.add (aggiorna_button)
      aggiorna.button.connect ("clicked", self.aggiorna)
    
    def _draw (self, widget, cr):
        r = random.randint (0,10)/10.0
        g = random.randint (0,10)/10.0
        b = random.randint (0,10)/10.0
        cr.set_source_rgb (r,g,b)
        cr.rectangle (0,0, widget.get_allocated_width(), widget.get_allocated_height())
        cr.fill ()
    
    def aggiorna (self, widget):
       for darea in self.square_list:
            darea.queue_draw()