Files
lightgui_python/examples/xstackbar.py
T
Muse 2ecc09dd4d Examples + shots: XStackBar, XVerticalBar, XTachometer, XArcGauge, XDonutGauge
Designer-pattern examples + Xvfb :107 screenshots (see Batch B
for the harness/shooter). Gauges re-shot after the _arc_stroke
fix; tachometer readout moved clear of the 0/max labels.
2026-12-05 17:07:59 +00:00

170 lines
4.9 KiB
Python

#!/usr/bin/env python3
# Generated by LightGUI (Python/XFrame).
# A plain Python/GTK program: no CMake. Run with:
# python3 xstackbar.py
# with the LightGUI Python package on PYTHONPATH
# (the designer sets it when it runs this script).
#
# The window is 'XStackBar example' (352x80) with 1 widget(s).
import sys
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk, Gtk # noqa: E402
from lightgui.xframe.core import create, parse_color # noqa: E402
def on_XStackBar_001_OnClick(widget):
print("EVENT %s %s" % (widget.name, 'OnClick'))
# Event handlers, wired to the widgets in main():
_HANDLERS = {
('XStackBar_001', 'OnClick'): on_XStackBar_001_OnClick,
}
# Design-time geometry + effective anchors per widget:
# name -> (top, left, right, bottom, x, y, width, height)
_ANCHORS = {
'XStackBar_001': (True, True, False, False, 16, 16, 320, 48),
}
def _apply_anchors(widgets, width, height):
"""Reposition anchored widgets for a drawing area of
*width* x *height* (Flag 1: minimal anchor layout).
Computed from the design geometry every time, so a
resize is idempotent — no drift across resizes."""
design_w, design_h = _DESIGN_SIZE
dx = width - design_w
dy = height - design_h
for widget in widgets:
top, left, right, bottom, ox, oy, ow, oh = \
_ANCHORS[widget.name]
x, y, w, h = ox, oy, ow, oh
if right and not left:
x = ox + dx
elif right and left:
w = ow + dx
if bottom and not top:
y = oy + dy
elif bottom and top:
h = oh + dy
widget.move_to(x, y)
widget.resize_to(w, h)
def _build_widgets():
"""Create every placed widget with its props."""
widgets = []
widget = create('XStackBar', 'XStackBar_001', 16, 16, 320, 48)
widget.set_prop("label", "Memory")
widget.set_prop("segments", "used=45|cached=25|buffers=10|free=20")
widgets.append(widget)
return widgets
_DESIGN_SIZE = (352, 80)
_BACKGROUND = ''
_WIDGETS = []
_LAST_SIZE = [None]
_PRESSED = [None] # Task 4: widget that got the ButtonPress
def _hit_widget(widgets, x, y):
"""Topmost visible widget containing (*x*, *y*)."""
for widget in reversed(widgets):
if widget.get_prop("visible", "true") in ("false", "0"):
continue
if widget.contains(x, y):
return widget
return None
def _dispatch_to(widget, event):
"""Call the *event* handler of *widget* (already chosen)."""
if widget is None:
return
if widget.get_prop("enabled", "true") in ("false", "0"):
return
handler = _HANDLERS.get((widget.name, event))
if handler is not None:
handler(widget)
def _dispatch(widgets, x, y, event):
"""Call the *event* handler of the widget at (*x*, *y*),
if it has one wired."""
_dispatch_to(_hit_widget(widgets, x, y), event)
def _on_draw(area, cr):
bg = parse_color(_BACKGROUND, None)
if bg is None:
bg = (1.0, 1.0, 1.0)
cr.set_source_rgb(*bg)
cr.paint()
for widget in _WIDGETS:
widget.draw(cr)
return False
def _on_button_press(area, event):
if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS:
_dispatch(_WIDGETS, event.x, event.y, "OnDblClick")
elif event.type == Gdk.EventType.BUTTON_PRESS:
_dispatch(_WIDGETS, event.x, event.y, "OnMouseDown")
_dispatch(_WIDGETS, event.x, event.y, "OnClick")
# Task 4: remember the pressed widget; release goes there.
_PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y)
return False
def _on_button_release(area, event):
# Task 4: release goes to the widget that got the press,
# even if the pointer moved off it.
widget = _PRESSED[0]
_PRESSED[0] = None
_dispatch_to(widget, "OnMouseUp")
return False
def _on_area_resize(area, allocation):
size = (allocation.width, allocation.height)
if size == _LAST_SIZE[0]:
return False
_LAST_SIZE[0] = size
_apply_anchors(_WIDGETS, size[0], size[1])
area.queue_draw()
return False
def main():
window = Gtk.Window(title='XStackBar example')
window.set_default_size(352, 80)
window.connect("destroy", Gtk.main_quit)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
spacing=0)
window.add(vbox)
area = Gtk.DrawingArea()
area.set_hexpand(True)
area.set_vexpand(True)
area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
Gdk.EventMask.BUTTON_RELEASE_MASK)
area.connect("draw", _on_draw)
area.connect("button-press-event", _on_button_press)
area.connect("button-release-event", _on_button_release)
area.connect("size-allocate", _on_area_resize)
vbox.pack_start(area, True, True, 0)
_WIDGETS.extend(_build_widgets())
window.show_all()
Gtk.main()
return 0
if __name__ == "__main__":
sys.exit(main())