dev/gtk/ PyGtkExamples1


Simple empty window

#!/usr/bin/env python3

idea = """
one thread sends a message to queue every second.
the other thread runs the glib main loop.
the loop has an idle process that checks for messages in the queue.
if it finds a message, it prints it out.
"""

import sys
import gi

gi.require_version("Gtk","3.0")
from gi.repository import Gtk

window = Gtk.Window(title="Hello World")
window.show()
window.connect("destroy",Gtk.main_quit)

Gtk.main()

Simple empty window with red background

It took quite a bit of digging around and googling to find out how to do this. Once you get the idea, it's straightforward, and like CSS with the DOM.

#!/usr/bin/env python3

idea = """
one thread sends a message to queue every second.
the other thread runs the glib main loop.
the loop has an idle process that checks for messages in the queue.
if it finds a message, it prints it out.
"""

import sys
import gi

gi.require_version("Gtk","3.0")
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk

colors = ["#7f0000","#007f00","#00007f"]
color_idx = 0
def next_color():
  global color_idx
  color_idx = (color_idx + 1) % len(colors)
  return colors[color_idx]

window = Gtk.Window(title="Hello World")
window.show()

cssProvider = Gtk.CssProvider()
cssProvider.load_from_data(""".flibble { background-color: red; }""".encode())
styleContext = window.get_style_context()
styleContext.add_provider(cssProvider,Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
styleContext.add_class("flibble")

window.connect("destroy",Gtk.main_quit)

Gtk.main()