如何在 gtk 窗口上绘图而不生成 Exposure 事件
我正在 PyGtk 中做一个项目,用于监视系统的性能。 我已经出于目的开发了几乎所有代码。但现在我遇到了问题。 我的问题是,我正在重新绘制公开事件上的所有内容,并且我想在窗口上绘制而不生成公开事件。原因是,我正在公开事件上绘制图表,但是当我调整窗口大小时,它没有显示完美的阅读,我想在窗口空闲且窗口未调整大小时绘制图表,并且当窗口调整大小时它不应该绘制图表。我尝试了配置事件,但对我来说没有成功。 请帮我。 请尽快回复我。 谢谢
这是我的完整代码:
import pygtk
pygtk.require(`2.0`)
import gtk
import cairo
import gobject
import random, sys, thread, time
from gtk import gdk
class Monitor(gtk.DrawingArea):
##__gsignals__ = {
##"configure-event" : "override"
##}
count = 0
coord = []
mem_coord = []
x1, y1 = 0, 0
str1 = []
max_mem = 0
mem_usage = 0
name = ""
G = 2
def __init__(self,parent,name):
super(Monitor,self).__init__()
self.coord.append(0)
self.connect("expose-event",self.do_on_expose)
self.name = name
gobject.timeout_add(1000,self.draw_loop)
def get_mem_data(self):
f = open("/proc/meminfo","r")
str2 = f.readline().split(" ")[:]
self.max_mem = int(str2[len(str2)-2])
for i in range(5):
str2 = f.readline().split(" ")[:]
str3 = int(str2[len(str2)-2])
f.close()
return str3
def get_mem_usage(self):
self.mem_usage = self.get_data()
val = (self.mem_usage * 100) / self.max_mem
return val
def get_cpu_data(self):
f = open("/proc/stat","r")
str2 = f.readline().split(" ")[2:6]
f.close()
for i in range(len(str2)):
str2[i] = int(str2[i])
return str2
def get_cpu_usage(self):
str2 = self.get_data()
for i in range(len(self.str1)):
self.str1[i] = str2[i] - self.str1[i]
val = 100 - (self.str1[len(self.str1) - 1] * 100.00 / sum(self.str1))
for i in range(len(self.str1)):
self.str1[i] = str2[i]
return val
def draw_loop(self):
self.queue_draw()
return True
def do_configure_event(self, event):
self.cr=self.window.cairo_create()
self.draw_grid(self.G)
self.draw_graph()
gtk.Window.do_configure_event(self, event)
def do_on_expose(self,widget,event):
self.cr=widget.window.cairo_create()
self.draw_grid(self.G)
self.draw_graph()
def draw_graph(self):
val = random.randint(1,100)##self.get_usage(self.resource) ##random.randint(1,100)
print "val = ",val
if self.count<256 :
self.count=self.count+1
self.coord.append(val)
else:
self.coord.pop(0)
self.coord.append(val)
val = (self.y1+self.y2-3) - ((val*(self.y2-6))/100)
x=self.x1 + self.x2 - 3
self.cr.move_to(x,val)
self.cr.set_source_rgb(255,0,0)
for i in range(self.count,-1,-1):
if x > self.x1+3:
val = (self.y1+self.y2-3) - ((self.coord[i]*(self.y2-6))/100)
self.cr.line_to(x,val)
self.cr.move_to(x,val)
x=x-5
else:
break
self.cr.stroke()
return False
def draw_grid(self,G):
size=self.window.get_size()
height = (size[1] / G) - 30
self.x1, self.y1 = 20, 20
self.x2, self.y2 = size[0]-(2*self.x1), height
x1, y1, x2, y2 = self.x1, self.y1, self.x2, self.y2
for i in range(G):
self.cr.set_source_rgb(0,0,0)
self.cr.rectangle(x1,y1,x2,y2)
self.cr.fill()
self.cr.stroke()
self.cr.set_source_rgb(0,255,0)
x3 = x2 - 6
y3 = y2 - 6
self.cr.rectangle(x1+3,y1+3,x3,y3)
self.cr.stroke()
x = x1 + 3
y = y1 + y2 - 3
fraction = int( round( x3 / 12.34 ) )
split = float(x3) / float(fraction)
self.cr.set_source_rgba(0,255,0,0.3)
for i in range(fraction):
x = x + split
self.cr.move_to(x,y1+3)
self.cr.line_to(x,y)
self.cr.stroke()
x = x1 + x2 - 3
y = y1 + 3
fraction = int( round( y3 / 12.34 ) )
split = float(y3) / float(fraction)
self.cr.set_source_rgba(0,255,0,0.3)
for i in range(fraction):
y = y + split
self.cr.move_to(x1+3,y)
self.cr.line_to(x,y)
self.cr.stroke()
y1 = y1 + y2 + 20
class main(gtk.Window):
__gsignals__ = {
"configure-event" : "override"
}
def __init__(self):
super(main,self).__init__()
self.set_title("Virtual Machine Monitor")
self.set_default_size(640,640)
self.set_resizable(True)
self.set_geometry_hints(min_width=640,min_height=500)
self.set_position(gtk.WIN_POS_CENTER)
self.connect("delete_event",gtk.main_quit)
##self.maximize()
vbox=gtk.VBox(False,1)
self.monitor2=Monitor(self,"MEM")
##self.monitor3=Monitor(self,"MEM")
vbox.pack_start(self.monitor2,1,1,0)
##vbox.pack_start(self.monitor3,1,1,0)
self.add(vbox)
self.show_all()
def do_configure_event(self, event):
title = "%s, %s" % (event.x, event.y)
##self.set_title(title)
gtk.Window.do_configure_event(self, event)
if __name__ == "__main__":
main()
gtk.main()
I am doing a project in PyGtk for monitoring a performance of system.
I have developed almost of the code for purpose.But now i have a problem in it.
My problem is that i am repainting all my stuff on expose-event and i want to draw on window without generating expose event.Reason for that is, i am plotting a graph on expose-event,but when i resize window it is not showing the perfect reading, i want to plot graph when window is idle and window is not resizing and when window gets resize it should not plot a graph.I tried for configure event but doesn't success to me.
please help me.
Please reply me soon.
thank you
here is my complete code:
import pygtk
pygtk.require(`2.0`)
import gtk
import cairo
import gobject
import random, sys, thread, time
from gtk import gdk
class Monitor(gtk.DrawingArea):
##__gsignals__ = {
##"configure-event" : "override"
##}
count = 0
coord = []
mem_coord = []
x1, y1 = 0, 0
str1 = []
max_mem = 0
mem_usage = 0
name = ""
G = 2
def __init__(self,parent,name):
super(Monitor,self).__init__()
self.coord.append(0)
self.connect("expose-event",self.do_on_expose)
self.name = name
gobject.timeout_add(1000,self.draw_loop)
def get_mem_data(self):
f = open("/proc/meminfo","r")
str2 = f.readline().split(" ")[:]
self.max_mem = int(str2[len(str2)-2])
for i in range(5):
str2 = f.readline().split(" ")[:]
str3 = int(str2[len(str2)-2])
f.close()
return str3
def get_mem_usage(self):
self.mem_usage = self.get_data()
val = (self.mem_usage * 100) / self.max_mem
return val
def get_cpu_data(self):
f = open("/proc/stat","r")
str2 = f.readline().split(" ")[2:6]
f.close()
for i in range(len(str2)):
str2[i] = int(str2[i])
return str2
def get_cpu_usage(self):
str2 = self.get_data()
for i in range(len(self.str1)):
self.str1[i] = str2[i] - self.str1[i]
val = 100 - (self.str1[len(self.str1) - 1] * 100.00 / sum(self.str1))
for i in range(len(self.str1)):
self.str1[i] = str2[i]
return val
def draw_loop(self):
self.queue_draw()
return True
def do_configure_event(self, event):
self.cr=self.window.cairo_create()
self.draw_grid(self.G)
self.draw_graph()
gtk.Window.do_configure_event(self, event)
def do_on_expose(self,widget,event):
self.cr=widget.window.cairo_create()
self.draw_grid(self.G)
self.draw_graph()
def draw_graph(self):
val = random.randint(1,100)##self.get_usage(self.resource) ##random.randint(1,100)
print "val = ",val
if self.count<256 :
self.count=self.count+1
self.coord.append(val)
else:
self.coord.pop(0)
self.coord.append(val)
val = (self.y1+self.y2-3) - ((val*(self.y2-6))/100)
x=self.x1 + self.x2 - 3
self.cr.move_to(x,val)
self.cr.set_source_rgb(255,0,0)
for i in range(self.count,-1,-1):
if x > self.x1+3:
val = (self.y1+self.y2-3) - ((self.coord[i]*(self.y2-6))/100)
self.cr.line_to(x,val)
self.cr.move_to(x,val)
x=x-5
else:
break
self.cr.stroke()
return False
def draw_grid(self,G):
size=self.window.get_size()
height = (size[1] / G) - 30
self.x1, self.y1 = 20, 20
self.x2, self.y2 = size[0]-(2*self.x1), height
x1, y1, x2, y2 = self.x1, self.y1, self.x2, self.y2
for i in range(G):
self.cr.set_source_rgb(0,0,0)
self.cr.rectangle(x1,y1,x2,y2)
self.cr.fill()
self.cr.stroke()
self.cr.set_source_rgb(0,255,0)
x3 = x2 - 6
y3 = y2 - 6
self.cr.rectangle(x1+3,y1+3,x3,y3)
self.cr.stroke()
x = x1 + 3
y = y1 + y2 - 3
fraction = int( round( x3 / 12.34 ) )
split = float(x3) / float(fraction)
self.cr.set_source_rgba(0,255,0,0.3)
for i in range(fraction):
x = x + split
self.cr.move_to(x,y1+3)
self.cr.line_to(x,y)
self.cr.stroke()
x = x1 + x2 - 3
y = y1 + 3
fraction = int( round( y3 / 12.34 ) )
split = float(y3) / float(fraction)
self.cr.set_source_rgba(0,255,0,0.3)
for i in range(fraction):
y = y + split
self.cr.move_to(x1+3,y)
self.cr.line_to(x,y)
self.cr.stroke()
y1 = y1 + y2 + 20
class main(gtk.Window):
__gsignals__ = {
"configure-event" : "override"
}
def __init__(self):
super(main,self).__init__()
self.set_title("Virtual Machine Monitor")
self.set_default_size(640,640)
self.set_resizable(True)
self.set_geometry_hints(min_width=640,min_height=500)
self.set_position(gtk.WIN_POS_CENTER)
self.connect("delete_event",gtk.main_quit)
##self.maximize()
vbox=gtk.VBox(False,1)
self.monitor2=Monitor(self,"MEM")
##self.monitor3=Monitor(self,"MEM")
vbox.pack_start(self.monitor2,1,1,0)
##vbox.pack_start(self.monitor3,1,1,0)
self.add(vbox)
self.show_all()
def do_configure_event(self, event):
title = "%s, %s" % (event.x, event.y)
##self.set_title(title)
gtk.Window.do_configure_event(self, event)
if __name__ == "__main__":
main()
gtk.main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 GTK 的 freeze_updates 和 thaw_updates 方法:
在 PyGTK 的 gdk.window 文档中查找该方法
You could use the freeze_updates and thaw_updates methods of GTK:
Look for that methods in gdk.window documentation at PyGTK
您可以尝试先隐藏窗口,然后再在窗口上绘图,从而在您
.show
时生成公开内容。常见问题解答可以在此处找到。
You might try .
hide
'ing the window before drawing on it, and thus generating the expose when you.show
it.The FAQ answer can be found here.