dev/gtk/ PyGtkExamples1


Simple empty window

#!/usr/bin/env python3

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

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

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