demo_anchorlayout.py
changeset 62 2f61931520c9
child 71 cfd3445107b4
equal deleted inserted replaced
61:15088f62c4ac 62:2f61931520c9
       
     1 #!/usr/bin/env python3
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 import locale
       
     5 import os
       
     6 
       
     7 from tuikit.application import Application
       
     8 from tuikit.window import Window
       
     9 from tuikit.label import Label
       
    10 from tuikit.button import Button
       
    11 from tuikit.layout import AnchorLayout
       
    12 from tuikit.common import Borders
       
    13 
       
    14 
       
    15 class MyApplication(Application):
       
    16     def __init__(self):
       
    17         Application.__init__(self)
       
    18         self.top = AnchorLayout()
       
    19         self.top.name = 'top'
       
    20         self.top.add_handler('keypress', self.on_top_keypress)
       
    21 
       
    22         win = Window()
       
    23         win.name = 'window'
       
    24         win.title = 'AnchorLayout demo'
       
    25         win.resize(80, 25)
       
    26         self.top.add(win, halign='left', valign='top')
       
    27         self.win = win
       
    28 
       
    29         button_valign = Button('valign: ' + self.win.hints['valign'].selected)
       
    30         button_valign.name = 'button_valign'
       
    31         button_valign.add_handler('click', self.on_button_align_click)
       
    32         win.add(button_valign, halign='center', margin=Borders(t=2))
       
    33 
       
    34         button_halign = Button('halign: ' + self.win.hints['halign'].selected)
       
    35         button_halign.name = 'button_halign'
       
    36         button_halign.add_handler('click', self.on_button_align_click)
       
    37         win.add(button_halign, halign='center', margin=Borders(t=4))
       
    38 
       
    39         label_margin = Label(str(self.win.hints['margin']))
       
    40         label_margin.name = 'label_margin'
       
    41         label_margin.add_handler('draw', self.on_label_margin_draw)
       
    42         win.add(label_margin, halign='center', margin=Borders(t=6))
       
    43 
       
    44     def on_button_align_click(self, ev):
       
    45         align_type = ev.originator.label.split(':', 1)[0]
       
    46         align = self.win.hints[align_type]
       
    47         align.select_next()
       
    48         ev.originator.label = '%s: %s' % (align_type, align.selected)
       
    49         self.top.emit('resize')
       
    50         return True
       
    51 
       
    52     def on_label_margin_draw(self, ev):
       
    53         ev.originator.label = str(self.win.hints['margin'])
       
    54 
       
    55     def on_top_keypress(self, ev):
       
    56         if ev.keyname == 'escape':
       
    57             self.terminate()
       
    58             return True
       
    59 
       
    60 
       
    61 if __name__ == '__main__':
       
    62     locale.setlocale(locale.LC_ALL, '')
       
    63     os.environ['ESCDELAY'] = '25' # do not wait 1 second after pressing Escape key
       
    64     app = MyApplication()
       
    65     app.start()