Skip to content

Instantly share code, notes, and snippets.

@thorsummoner
Last active January 18, 2016 06:23
Show Gist options
  • Save thorsummoner/b7dff83b91ab109e2d99 to your computer and use it in GitHub Desktop.
Save thorsummoner/b7dff83b91ab109e2d99 to your computer and use it in GitHub Desktop.
Pygtk3 window boilerplate
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
# PyBuilder
target/
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
</interface>
#!/usr/bin/env python
import gladewindow
import os
class ExampleWindow(gladewindow.BaseWindow):
"""
Gui application interface.
"""
GLADE_FILE = os.path.join(os.path.dirname(__file__), '.glade')
def __init__(self):
super(ExampleWindow, self).__init__()
class Handler(gladewindow.BaseHandler):
"""
Main Window Event Handler
"""
if __name__ == '__main__':
exit(ExampleWindow().main())
#!/usr/bin/env python
"""
Main Window Class
"""
import os
import signal
from gi.repository import Gtk
from gi.repository import Gdk
class BaseWindow(Gtk.Window):
"""
Gui application interface.
"""
# pylint: disable=no-member
ROOT_WINDOW = 'window1'
GLADE_FILE = None
CSS_PROVIDER_FILE = None
def __init__(self, *args):
super(BaseWindow, self).__init__(*args)
builder = Gtk.Builder()
builder.add_from_file(self.GLADE_FILE)
self.builder = builder
self.window = self.builder.get_object(self.ROOT_WINDOW)
self.builder.connect_signals(self.Handler(self))
if self.CSS_PROVIDER_FILE is not None:
self.cssprovider = Gtk.CssProvider()
self.cssprovider.load_from_path(self.CSS_PROVIDER_FILE)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
self.cssprovider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
self.window.show_all()
@staticmethod
def main():
"""
Gtk.main wrapper.
"""
signal.signal(signal.SIGINT, signal.SIG_DFL)
print('Main Enter')
Gtk.main()
print('Main Exit')
@staticmethod
def replace_child(container, new_child):
"""
Remove all elements from container and add an element
to the container.
"""
for child in container.get_children():
container.remove(child)
container.add(new_child)
class BaseHandler(object):
"""
Main Window Event Handler
"""
def __init__(self, parent):
super(BaseHandler, self).__init__()
self.parent = parent
parent.window.connect("delete-event", self.on_delete_window)
@staticmethod
def on_delete_window(*args):
"""
Window Close Action
"""
Gtk.main_quit(*args)
# pylint: enable=no-member
@staticmethod
def draw(widget, ct):
"""
Draw Diagnostic Marks
"""
width = widget.get_allocated_width()
height = widget.get_allocated_height()
ct.set_source_rgb(0, 0, 0)
ct.move_to(0 * width, 0 * height)
ct.line_to(1 * width, 1 * height)
ct.move_to(1 * width, 0 * height)
ct.line_to(0 * width, 1 * height)
ct.set_line_width(20)
ct.stroke()
ct.rectangle(0*width, 0*height, 0.5*width, 0.5*height)
ct.set_source_rgba(1, 0, 0, 0.80)
ct.fill()
ct.rectangle(0*width, 0.5*height, 0.5*width, 0.5*height)
ct.set_source_rgba(0, 1, 0, 0.60)
ct.fill()
ct.rectangle(0.5*width, 0*height, 0.5*width, 0.5*height)
ct.set_source_rgba(0, 0, 1, 0.40)
ct.fill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment