Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
#!/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.hint_value('valign'))
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.hint_value('halign'))
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.hint_value('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))
label_size = Label(str(self.win.sizereq))
label_size.name = 'label_size'
label_size.add_handler('draw', self.on_label_size_draw)
win.add(label_size, halign='center', margin=Borders(t=8))
def on_button_align_click(self, ev):
align_type = ev.originator.label.split(':', 1)[0]
align = self.win.get_hint(align_type)
align.select_next()
ev.originator.label = '%s: %s' % (align_type, align.selected)
self.top.need_resize()
return True
def on_label_margin_draw(self, ev):
ev.originator.label = str(self.win.hint_value('margin'))
def on_label_size_draw(self, ev):
ev.originator.label = str(self.win.sizereq)
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()