2011-10-09 84 views
1

我在自定義小部件顯示在單獨的窗口中遇到問題。單獨的窗口不在窗口中的所有窗口小部件的意義上,但它們都是單獨出現自己的窗口裝飾。我不確定問題會發生在哪裏,但我猜想它可能出現在以下之一中,也許與我如何創建窗口有關。 do_realize在init(它也出現在下面)中被調用,並且我認爲do_expose_event在被要求繪製自己時被gtk調用,但是我不太確定(我對這一切都很陌生)。我在do_expose_event中放置了一個打印語句,並且在打印語句之後,每次打印語句之後我都會調用它,這些打印語句是在創建小部件的程序中輸入gtk main之前立即放置的。所有代碼都是GPLed。pygtk自定義小部件顯示在單獨的窗口

如果需要其他代碼,可以根據要求提供。

def do_realize(self): 
    """Makes a window on which we draw""" 

    #set a flag to say we are realised 
    self.set_flags(self.flags() | gtk.REALIZED) 

    #make a gdk window to draw on 
    self.window = gtk.gdk.Window(self.get_parent_window(), width=self.allocation.width, 
        height=self.allocation.height, window_type=gtk.gdk.WINDOW_CHILD, 
        wclass=gtk.gdk.INPUT_OUTPUT, event_mask=self.get_events() | gtk.gdk.EXPOSURE_MASK) 

    #associate the window with the widget 
    self.window.set_user_data(self) 

    #attach a style 
    self.style.attach(self.window) 

    #set the default background colour to whatever the theme says, and the state 
    self.style.set_background(self.window, gtk.STATE_NORMAL) 
    self.window.move_resize(*self.allocation) 

def do_expose_event(self, event): 
    """Called when the widget is asked to draw itself""" 
    #put the allocation in some variables 
    x, y, w, h = self.allocation 
    cr = self.window.cairo_create() 


    #if height is greater than width 
    if (h > w): 
     scale = h/float(BASE_SIZE) 
    else: 
     scale = w/float(BASE_SIZE) 

    #set the scale 
    cr.scale(scale,scale) 

    #put in the colour 
    self.draw_background_color(cr) 

    #draw the highlight if necessary 
    if self.is_focus(): 
     self.draw_highlight_box(cr) 

    self.draw_normal_box(cr) 
    self.draw_text(cr) 
    if self.draw_boxes and self.is_focus(): 
     self.draw_note_area_highlight_box(cr) 

def __init__(self, upper=9, text=''): 
    """initialise it""" 
    gtk.Widget.__init__(self)  #init the super class 
    self.upper = upper 
    self.font = self.style.font_desc 
    self.font.set_size(pango.SCALE*13) 
    self.note_font = self.font.copy() 
    self.note_font.set_size(pango.SCALE*6) 

    #set properties-can focus and grab all events 
    self.set_property('can-focus',True) 
    self.set_property('events',gtk.gdk.ALL_EVENTS_MASK) 

    #connect the events to functions 
    self.connect('button-press-event',self.button_press)  #box is clicked on 
    self.connect('key-release-event',self.key_press)   #release of key when inputting 
    self.connect('enter-notify-event',self.pointer_enter)  #pointer enters 
    self.connect('leave-notify-event',self.pointer_leave)  #pointer leaves 
    self.connect('focus-in-event',self.focus_in)    #goes into focus 
    self.connect('focus-out-event',self.focus_out)    #goes out of focus 
    self.connect('motion-notify-event',self.motion_notify)  #poniter is in the window 

    #debugging info 
    #print type(self) 

    self.set_text(text) 
    self.do_realize() 
+0

這兩種方法從哪裏調用? – Louis

+0

它們都是從__init__中調用的,我改變了問題以使其更清晰。 – Harpy

回答

1

而是在「__初始化‘呼籲do_realize的(沒有’」和「初始化」之間的空間,它粗體文字),我應該叫queue_draw。我在pygtk irc上得到了幫助。

+0

我也想知道爲什麼do_realize在那裏......但沒有找到解決方案。感謝發佈。我對這兩條條目都贊成有用的消息。 – Louis

相關問題