demo_anchorlayout.py
author Radek Brich <radek.brich@devl.cz>
Wed, 23 Jan 2013 21:59:16 +0100
changeset 68 bc51b0220be2
parent 62 2f61931520c9
child 71 cfd3445107b4
permissions -rwxr-xr-x
DriverCurses: filter mousemove - generate event only when coordinates changes.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import locale
import os

from tuikit.application import Application
from tuikit.window import Window
from tuikit.label import Label
from tuikit.button import Button
from tuikit.layout import AnchorLayout
from tuikit.common import Borders


class MyApplication(Application):
    def __init__(self):
        Application.__init__(self)
        self.top = AnchorLayout()
        self.top.name = 'top'
        self.top.add_handler('keypress', self.on_top_keypress)

        win = Window()
        win.name = 'window'
        win.title = 'AnchorLayout demo'
        win.resize(80, 25)
        self.top.add(win, halign='left', valign='top')
        self.win = win

        button_valign = Button('valign: ' + self.win.hints['valign'].selected)
        button_valign.name = 'button_valign'
        button_valign.add_handler('click', self.on_button_align_click)
        win.add(button_valign, halign='center', margin=Borders(t=2))

        button_halign = Button('halign: ' + self.win.hints['halign'].selected)
        button_halign.name = 'button_halign'
        button_halign.add_handler('click', self.on_button_align_click)
        win.add(button_halign, halign='center', margin=Borders(t=4))

        label_margin = Label(str(self.win.hints['margin']))
        label_margin.name = 'label_margin'
        label_margin.add_handler('draw', self.on_label_margin_draw)
        win.add(label_margin, halign='center', margin=Borders(t=6))

    def on_button_align_click(self, ev):
        align_type = ev.originator.label.split(':', 1)[0]
        align = self.win.hints[align_type]
        align.select_next()
        ev.originator.label = '%s: %s' % (align_type, align.selected)
        self.top.emit('resize')
        return True

    def on_label_margin_draw(self, ev):
        ev.originator.label = str(self.win.hints['margin'])

    def on_top_keypress(self, ev):
        if ev.keyname == 'escape':
            self.terminate()
            return True


if __name__ == '__main__':
    locale.setlocale(locale.LC_ALL, '')
    os.environ['ESCDELAY'] = '25' # do not wait 1 second after pressing Escape key
    app = MyApplication()
    app.start()