我可以在单个Gtk.DrawingArea中绘制,但是当我尝试为很多人绘制时,例如50, 我在绘图中遇到了一些错误 。
以下是您需要查看的代码:
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获得随机颜色?
答案 0 :(得分:1)
**编辑**
示例代码:
....
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()