如何在gtk窗口上绘制而不生成公开事件

时间:2011-02-27 12:12:55

标签: python pygtk

我正在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()

2 个答案:

答案 0 :(得分:0)

您可以在绘制窗口之前尝试。hide,然后在.show时生成曝光。

可以找到常见问题解答答案here.

答案 1 :(得分:0)

您可以使用GTK的freeze_updates和thaw_updates方法:

# 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文档中查找这些方法