pygtk自定义小部件显示在单独的窗口中

发布于 2024-12-08 15:55:42 字数 3000 浏览 1 评论 0原文

我在单独的窗口中显示自定义小部件时遇到问题。单独的窗口并不是所有小部件都在一个窗口中的意义上,而是它们都单独出现并带有自己的窗口装饰。我不确定问题出在哪里,但我猜测可能是以下之一,可能与我创建窗口的方式有关。 do_realize 在 init 中被调用(也出现在下面),我认为 do_expose_event 在 gtk 被要求绘制自身时被调用,但我不太确定(我对这一切都很陌生) )。我在 do_expose_event 中放置了一个打印语句,并且在我在制作小部件的程序中进入 gtk main 之前放置的每个打印语句之后调用它。所有代码都是 GPL 的。

如果需要任何其他代码,可根据要求提供。

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

I'm having trouble with custom widgets showing up in a separate window. Separate window not in the sense of all widgets being in a window, but they all come up separately with their own window decorations. I'm not sure where the problem would be, but I guessed it might be in one of the following, maybe to do with how I'm creating the window. do_realize is called in init (which also appears below), and I think do_expose_event is called by gtk when it's asked to draw itself, but I'm not really sure (I'm very new to all this). I put a print statement in do_expose_event, and it is called for each after a print statement that I placed immediately before entering gtk main in the program that makes the widgets. All of the code is GPLed.

If any other code is needed it's available on request.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软的没边 2024-12-15 15:55:42

我应该调用 queue_draw,而不是在 __init__ 中调用 do_realize。我在 pygtk irc 上得到了帮助。

Instead of calling do_realize in __init__, I should be calling queue_draw. I got help on the pygtk irc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文