2011-02-27 51 views
1

我正在PyGtk中執行一個用於監視系統性能的項目。 我已經開發了幾乎所有的代碼,但現在我遇到了一個問題。 我的問題是,我正在重新繪製所有我在暴露事件的東西,我想在窗口上繪製而不生成暴露事件。原因是,我正在繪製一個關於暴露事件的圖形,但是當我調整窗口大小時,它不是顯示完美的閱讀,我想繪製圖表當窗口閒置和窗口不調整大小,當窗口調整大小時,它不應該繪製一個圖形。我嘗試配置事件,但沒有成功給我。 請幫助我。 請儘快回覆。 謝謝如何在不生成暴露事件的情況下在gtk窗口上繪製

這裏是我的完整代碼:

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

回答

0

您可以使用freeze_updates和thaw_updates 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() 

查找在PyGTK的

gdk.window文檔中該方法
0

您可以試試。 hide'在窗口上繪製它之前,因此產生暴露當你.show它。

的常見問題的答案都可以找到here.

相關問題