Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python3 Gtk3 it is, then:</p> <pre class="lang-py prettyprint-override"><code>from gi.repository import Gtk class SuperSimpleWidget(Gtk.Label): __gtype_name__ = 'SuperSimpleWidget' </code></pre> <p>Here is a non-trivial example that actually does something, namely paints its background and draws a diagonal line through it. I'm inheriting from <code>Gtk.Misc</code> instead of <code>Gtk.Widget</code> to save some boilerplate (see below):</p> <pre class="lang-py prettyprint-override"><code>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 a 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 the widget cr.line_to(allocation.width, allocation.height) cr.stroke() </code></pre> <p>Finally, if you really want to derive from <code>Gtk.Widget</code> then you also have to set up a drawing background. <code>Gtk.Misc</code> does that for you, but <code>Gtk.Widget</code> could be a container that doesn't actually draw anything itself. But inquiring minds want to know, so you <strong>could</strong> do it like so:</p> <pre class="lang-py prettyprint-override"><code>from gi.repository import Gdk class ManualWidget(Gtk.Widget): __gtype_name__ = 'ManualWidget' def __init__(self, *args, **kwds): # same as above def do_draw(self, cr): # same as 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) </code></pre> <p><strong>Edit</strong> and to actually use it:</p> <pre class="lang-py prettyprint-override"><code>w = Gtk.Window() w.add(SimpleWidget()) w.show_all() w.present() import signal # enable Ctrl-C since there is no menu to quit signal.signal(signal.SIGINT, signal.SIG_DFL) Gtk.main() </code></pre> <p>Or, more fun, use it directly from the ipython3 REPL:</p> <pre class="lang-py prettyprint-override"><code>from IPython.lib.inputhook import enable_gtk3 enable_gtk3() w = Gtk.Window() w.add(SimpleWidget()) w.show_all() w.present() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload