FrancescoN
FrancescoN

Reputation: 2186

unable to draw correctly in Gtk+3

I can draw in a single Gtk.DrawingArea, but when i try to do it for many, for example 50, i got some errors in drawing.

enter image description here

Here is the code you need to check out:

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) why do i get errors in drawing?

2) why do Gtk.DrawingArea(s) change color ONLY when i update the window (for example i switch from a program to Gtk.DrawingArea window)?

3) why don't i get random colors for each Gtk.DrawingArea?

Upvotes: 0

Views: 614

Answers (1)

luciomrx
luciomrx

Reputation: 1195

  1. cant help you on this
  2. because it only changes color when Gtk.DrawingArea repainted itself ("draw" signal)
  3. the r,g,b should be inside "draw" function. you did construct r,g,b but since it's outside the draw function, it did not change while area repainted.
  4. why the sleep?

** edited **

sample code:

  ....
  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()

Upvotes: 1

Related Questions