gtk - Write custom widget with GTK3 -


i trying find simplest example of custom widget being written gtk-3.

so far best thing i've found this (using pygtk), seems targeted gtk-2.

btw: don't care language written in, if can avoid c++, better!

python3 gtk3 is, then:

from gi.repository import gtk  class supersimplewidget(gtk.label):     __gtype_name__ = 'supersimplewidget' 

here non-trivial example something, namely paints background , draws diagonal line through it. i'm inheriting gtk.misc instead of gtk.widget save boilerplate (see below):

class simplewidget(gtk.misc):     __gtype_name__ = 'simplewidget'      def __init__(self, *args, **kwds):         super().__init__(*args, **kwds)         self.set_size_request(40, 40)      def do_draw(self, cr):         # paint background         bg_color = self.get_style_context().get_background_color(gtk.stateflags.normal)         cr.set_source_rgba(*list(bg_color))         cr.paint()         # draw diagonal line         allocation = self.get_allocation()         fg_color = self.get_style_context().get_color(gtk.stateflags.normal)         cr.set_source_rgba(*list(fg_color));         cr.set_line_width(2)         cr.move_to(0, 0)   # top left of widget         cr.line_to(allocation.width, allocation.height)         cr.stroke() 

finally, if want derive gtk.widget have set drawing background. gtk.misc you, gtk.widget container doesn't draw itself. inquiring minds want know, could so:

from gi.repository import gdk  class manualwidget(gtk.widget):     __gtype_name__ = 'manualwidget'      def __init__(self, *args, **kwds):         # same above      def do_draw(self, cr):         # same above      def do_realize(self):         allocation = self.get_allocation()         attr = gdk.windowattr()         attr.window_type = gdk.windowtype.child         attr.x = allocation.x         attr.y = allocation.y         attr.width = allocation.width         attr.height = allocation.height         attr.visual = self.get_visual()         attr.event_mask = self.get_events() | gdk.eventmask.exposure_mask         wat = gdk.windowattributestype         mask = wat.x | wat.y | wat.visual         window = gdk.window(self.get_parent_window(), attr, mask);         self.set_window(window)         self.register_window(window)         self.set_realized(true)         window.set_background_pattern(none) 

edit , use it:

w = gtk.window() w.add(simplewidget()) w.show_all() w.present() import signal    # enable ctrl-c since there no menu quit signal.signal(signal.sigint, signal.sig_dfl) gtk.main() 

or, more fun, use directly ipython3 repl:

from ipython.lib.inputhook import enable_gtk3 enable_gtk3() w = gtk.window() w.add(simplewidget()) w.show_all() w.present() 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -