How draw on gtk window without generating expose event
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)
                            开发者_如何学C    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()
You could use the freeze_updates and thaw_updates methods of GTK:
# GTK Imports
import gtk
gdkwindow=mywindow.window
if gdkwindow:
    gdkwindow.freeze_updates()
# Do cairo drawing operations here
if gdkwindow:
    gdkwindow.thaw_updates()
window.queue_draw()
Look for that methods in gdk.window documentation at PyGTK
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.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论