Python pygtk自定义小部件显示在单独的窗口中
在单独的窗口中显示自定义小部件时,我遇到了问题。独立窗口不是指所有的窗口小部件都在一个窗口中,而是它们都有各自的窗口装饰。我不确定问题出在哪里,但我猜可能是在下面的一个问题中,可能与我如何创建窗口有关。do_realize在init中调用(也出现在下面),我认为当gtk要求它自己绘制时,do_expose_事件由gtk调用,但我不太确定(我对这一切都很陌生)。我在do_expose_事件中放置了一个print语句,并在我在生成小部件的程序中输入gtk main之前放置的print语句之后为每个语句调用它。所有代码都是GPLed 如果需要任何其他代码,可根据要求提供Python pygtk自定义小部件显示在单独的窗口中,python,gtk,widget,pygtk,pycairo,Python,Gtk,Widget,Pygtk,Pycairo,在单独的窗口中显示自定义小部件时,我遇到了问题。独立窗口不是指所有的窗口小部件都在一个窗口中,而是它们都有各自的窗口装饰。我不确定问题出在哪里,但我猜可能是在下面的一个问题中,可能与我如何创建窗口有关。do_realize在init中调用(也出现在下面),我认为当gtk要求它自己绘制时,do_expose_事件由gtk调用,但我不太确定(我对这一切都很陌生)。我在do_expose_事件中放置了一个print语句,并在我在生成小部件的程序中输入gtk main之前放置的print语句之后为每个语
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()
我应该调用queue\u draw,而不是在“\uuuu init”中调用do\u realize(在“”和“init”之间没有空格,而是将文本加粗)。我得到了pygtk irc的帮助。我应该调用queue\u draw,而不是在“\uuuuInit”中调用do\u realize(在“”和“init”之间没有空格)。我在pygtk irc上得到了帮助。这两个方法是从哪里调用的?它们都是从init调用的,我更改了问题以使其更清晰。这两个方法是从哪里调用的?它们都是从init调用的,我更改了问题以使其更清晰。我还想知道为什么你会意识到存在。。。但没有找到解决办法。谢谢你的发帖。我对这条有用的信息投了你的赞成票。我还想知道为什么你会意识到。。。但没有找到解决办法。谢谢你的发帖。我对你的两个条目都投了赞成票。