def __init__(self, font, *, elements, location=(20, 20), label=None, bgcolor=DARKGREEN, buttonwidth=25, closebutton=True): height = 150 spacing = 20 buttonwidth = max(max([TFT.get_stringsize(x[0], font)[0] for x in elements]) + 4, buttonwidth) buttonheight = max(TFT.get_stringsize('x', font)[1], 25) nelements = len(elements) width = spacing + (buttonwidth + spacing) * nelements if label is not None: width = max(width, TFT.get_stringsize(label, font)[0] + 2 * spacing) super().__init__(location, height, width, bgcolor = bgcolor) x = self.location[0] + spacing # Coordinates relative to physical display gap = 0 if nelements > 1: gap = ((width - 2 * spacing) - nelements * buttonwidth) // (nelements - 1) y = self.location[1] + self.height - buttonheight - 10 if label is not None: Label((x, self.location[1] + 50), font = font, bgcolor = bgcolor, value = label) for text, color in elements: Button((x, y), height = buttonheight, width = buttonwidth, font = font, fontcolor = BLACK, fgcolor = color, text = text, shape = RECTANGLE, callback = self.back, args = (text,)) x += buttonwidth + gap if closebutton: x, y = TFT.get_stringsize('X', font) size = max(x, y, 25) Button((self.location[0] + width - (size + 1), self.location[1] + 1), height = size, width = size, font = font, fgcolor = RED, text = 'X', shape = RECTANGLE, callback = self.back, args = ('Close',))
def __init__(self): height = 150 width = 220 super().__init__((20, 20), height, width, bgcolor=DARKGREEN) y = self.height - 50 Button(self.locn(20, y), height=30, width=80, font=font14, fontcolor=BLACK, fgcolor=RED, text='Cat', shape=RECTANGLE, callback=self.back, args=('Cat', )) Button(self.locn(120, y), height=30, width=80, font=font14, fontcolor=BLACK, fgcolor=GREEN, text='Dog', shape=RECTANGLE, callback=self.back, args=('Dog', )) Button(self.locn(width - 26, 1), height=25, width=25, font=font10, fgcolor=RED, text='X', shape=RECTANGLE, callback=self.back, args=('Close', ))
def __init__(self): super().__init__() fwdbutton(0, 242, BackScreen, 'Fast') fwdbutton(120, 242, TabScreen, 'Tabs') quitbutton() tb1 = Textbox((0, 0), 200, 8, font=IFONT16, **tbargs) tb2 = Textbox((210, 0), 200, 8, font=IFONT16, clip=False, **tbargs) Label((0, 135), width=200, value='Clipping', **labels) Label((210, 135), width=200, value='Wrapping', **labels) btns = (Button((0, 180), text='Up', callback=btn_cb, args=(tb1, tb2, 1), **btntable), Button((120, 180), text='Down', callback=btn_cb, args=(tb1, tb2, -1), **btntable)) for btn in btns: btn.greyed_out(True) # Disallow until textboxes are populated self.reg_task(txt_test(tb1, None)) self.reg_task(txt_test(tb2, btns))
def ctrl(tb, keys): # Create control keys table = { 'shape' : CIRCLE, 'fgcolor' : RED, 'fontcolor' : WHITE, 'litcolor' : YELLOW, } def shift(_): for k in keys: k.do_shift() Button((741, 55), font = font14, text = 'En', callback=bcb, args = (tb,), **table) Button((715, 0), font = font14, text = 'Bs', callback=bcb, args = (tb,), **table) Button((300, 220), font = font14, shape = CLIPPED_RECT, width = 200, height = 30, fgcolor = LIGHTGREEN, litcolor = YELLOW, callback = bcb, args = (tb,), text = " ") # Spacebar Button((0, 165), font = font14, text = 'Shift', callback=shift, shape = CLIPPED_RECT, fgcolor = RED, fontcolor = WHITE, litcolor = YELLOW)
def __init__(self): super().__init__() backbutton() tb = Textbox((0, 0), 200, 8, font=font10, clip=False, tab=64, **tbargs) Button((0, 180), text='Fill', callback=pop_tabs, args=(tb, ), **btntable) Button((120, 180), text='Clear', callback=lambda b, tb: tb.clear(), args=(tb, ), **btntable)
def quitbutton(): def quit(button): Screen.shutdown() return Button((390, 242), font=font10, callback=quit, height=25, width=80, fgcolor=RED, text='Quit')
def ovlbutton(): def fwd(button): Screen.change(BackScreen) return Button((400, 100), font=font10, fontcolor=BLACK, callback=fwd, fgcolor=CYAN, text='Fwd', height=25, width=70)
def fwdbutton(x, y, cls_screen, *, text='Next', args=[], kwargs={}): def fwd(button): Screen.change(cls_screen, args=args, kwargs=kwargs) Button((x, y), height=30, font=font14, callback=fwd, fgcolor=RED, text=text, shape=RECTANGLE, width=80)
def clearbutton(graph): def clear(button): graph.clear() return Button((400, 150), font=font10, fontcolor=BLACK, callback=clear, fgcolor=GREEN, text='Clear', height=25, width=70)
def backbutton(): def back(button): Screen.back() return Button((454, 0), font=font10, fontcolor=BLACK, callback=back, fgcolor=RED, text='X', height=25, width=25)
def fwdbutton(x, y, cls_screen, text='Next'): def fwd(button): Screen.change(cls_screen) Button((x, y), height=30, font=font14, callback=fwd, fgcolor=RED, text=text, shape=RECTANGLE, width=80)
def quitbutton(): def quit(button): Screen.shutdown() Button((769, 0), height=30, font=font14, callback=quit, fgcolor=RED, text='X', shape=RECTANGLE, width=30)
def quitbutton(x, y): def quit(button): Screen.shutdown() Button((x, y), height=30, font=font14, callback=quit, fgcolor=RED, text='Quit', shape=RECTANGLE, width=80)
def fwdbutton(x, y, screen, text='Next'): def fwd(button, screen): Screen.change(screen) return Button((x, y), font=font10, fontcolor=BLACK, callback=fwd, height=25, width=70, args=[screen], fgcolor=CYAN, text=text)
def clearbutton(self, graph): def clear(button): self.aqu_task.cancel() graph.clear() return Button((400, 150), font=font10, fontcolor=BLACK, callback=clear, fgcolor=GREEN, text='Clear', height=25, width=70)
def refreshbutton(curvelist): def refresh(button): for curve in curvelist: curve.show() return Button((400, 200), font=font10, fontcolor=BLACK, callback=refresh, fgcolor=GREEN, text='Refresh', height=25, width=70)
def backbutton(x, y): def back(button): Screen.back() Button((x, y), height=30, font=font14, fontcolor=BLACK, callback=back, fgcolor=CYAN, text='Back', shape=RECTANGLE, width=80)
def backbutton(self, x, y): def back(button): for task in self.tasks: task.cancel() self.tasks = [] Screen.back() Button((x, y), height=30, font=font14, fontcolor=BLACK, callback=back, fgcolor=CYAN, text='Back', shape=RECTANGLE, width=80)
def ctrl(uart, keys): # Create control keys table = { 'shape' : CIRCLE, 'fgcolor' : RED, 'fontcolor' : WHITE, 'litcolor' : YELLOW, } def shift(_): for k in keys: k.do_shift() Button((741, 55), font = font14, text = 'En', callback=bcb, args = (uart,), **table) Button((27, 55), font = font14, text = 'Tab', callback=bcb, args = (uart,), **table) Button((715, 0), font = font14, text = 'Bs', callback=bcb, args = (uart,), **table) Button((300, 225), font = font14, shape = CLIPPED_RECT, width = 200, height = 30, litcolor = YELLOW, fgcolor = LIGHTGREEN, callback = bcb, args = (uart,), text = " ") # Spacebar Button((715, 170), font = font14, width = 80, height = 40, text = 'Ctrl-c', callback=bcb, args = (uart,), shape = CLIPPED_RECT, fgcolor = RED, fontcolor = WHITE, litcolor = YELLOW) Button((715, 225), font = font14, width = 80, height = 40, text = 'Ctrl-d', callback=bcb, args = (uart,), shape = CLIPPED_RECT, fgcolor = RED, fontcolor = WHITE, litcolor = YELLOW) Button((715, 280), font = font14, width = 80, height = 40, text = 'Reset', callback=rst, shape = CLIPPED_RECT, fgcolor = RED, fontcolor = WHITE, litcolor = YELLOW) Button((0, 170), font = font14, text = 'Shift', height = 40, callback=shift, shape = CLIPPED_RECT, fgcolor = RED, fontcolor = WHITE, litcolor = YELLOW)
def fwdbutton(x, y, cls_screen, text='Next'): def fwd(button): Screen.change(cls_screen) Button((x, y), callback=fwd, fgcolor=GREEN, text=text, **buttons)
def __init__(self): super().__init__() # Common args for the labels labels = { 'width': 60, 'fontcolor': WHITE, 'border': 2, 'fgcolor': RED, 'bgcolor': (0, 40, 0), } # Common args for all three sliders table = { 'fontcolor': WHITE, 'legends': ('0', '5', '10'), 'cb_end': self.callback, } btnquit = Button((390, 240), font=font14, callback=quit, fgcolor=RED, text='Quit', shape=RECTANGLE, width=80, height=30) self.dial1 = Dial((350, 10), fgcolor=YELLOW, border=2, pointers=(0.9, 0.7)) self.dial2 = Dial((350, 120), fgcolor=YELLOW, border=2, pointers=(0.9, 0.7)) self.lstlbl = [] for n in range(3): self.lstlbl.append(Label((80 * n, 240), font=font10, **labels)) y = 5 self.slave1 = Slider((80, y), font=font10, fgcolor=GREEN, cbe_args=('Slave1', ), cb_move=self.slave_moved, cbm_args=(1, ), **table) self.slave2 = Slider((160, y), font=font10, fgcolor=GREEN, cbe_args=('Slave2', ), cb_move=self.slave_moved, cbm_args=(2, ), **table) master = Slider((0, y), font=font10, fgcolor=YELLOW, cbe_args=('Master', ), cb_move=self.master_moved, value=0.5, border=2, **table) self.reg_task(self.task1()) self.reg_task(self.task2()) # On/Off toggle: enable/disable quit button and one slider bs = ButtonList(self.cb_en_dis) lst_en_dis = [self.slave1, btnquit] button = bs.add_button((280, 240), font=font14, fontcolor=BLACK, height=30, width=90, fgcolor=GREEN, shape=RECTANGLE, text='Disable', args=[True, lst_en_dis]) button = bs.add_button((280, 240), font=font14, fontcolor=BLACK, height=30, width=90, fgcolor=RED, shape=RECTANGLE, text='Enable', args=[False, lst_en_dis])
def __init__(self): super().__init__() # Scale 0 with custom variable and legends. Label((0, 0), font=font14, value='FM radio scale 88-108MHz.') lbl_result0 = Label((0, 240), **labels) # Define callbacks for scale 0 def legendcb(f): return '{:2.0f}'.format(88 + ((f + 1) / 2) * (108 - 88)) def scale_move0(scale): sv = scale.value() sv = (sv + 1) / 2 # 0 <= sv <= 1 lbl_result0.value('{:6.2f}'.format(sv * (108 - 88) + 88)) self.scale0 = ScaleCtrl((0, 30), legendcb=legendcb, cb_move=scale_move0, **scales) # Scale 1 with varying color. Label((0, 130), font=font14, value='Default scale -1 to +1, varying colors.') lbl_result1 = Label((200, 240), **labels) # Define callbacks for scale 1 def tickcb(f, c): if f > 0.8: return RED if f < -0.8: return BLUE return c def scale_move1(scale): sv = scale.value() lbl_result1.value('{:4.3f}'.format(sv)) self.scale1 = ScaleCtrl((0, 160), tickcb=tickcb, cb_move=scale_move1, **scales) # Define buttons x = 390 y = 242 backbutton(x, y) #Button((x, y), fgcolor = RED, text = 'Quit', #callback = lambda _: Screen.shutdown(), **buttons) y -= 50 Button((x, y), fgcolor=GREEN, text='Enable', callback=self.en, **buttons) y -= 50 Button((x, y), fgcolor=YELLOW, text='Disable', callback=self.dis, **buttons) y -= 50 Button((x, y), fgcolor=BLUE, text='Zero', callback=lambda _: self.scale1.value(0), **buttons)
def quitbutton(): Button((719, 449), font = font14, height = 30, width = 80, shape = RECTANGLE, fgcolor = RED, text = 'Quit', callback = lambda _ : Screen.shutdown())
def quitbutton(): def quit(button): Screen.shutdown() Button((390, 240), font = font14, callback = quit, fgcolor = RED, text = 'Quit', width=80, height=30)
def __init__(self): super().__init__() # Scale 0 Label((0, 0), font=font14, value='Default scale 5 decades.') lbl_result0 = Label((0, 240), **labels) # Define callbacks for scale 0 def legendcb(f): if f < 999: return '{:<1.0f}'.format(f) return '{:<1.0f}K'.format(f / 1000) def scale_move0(scale): sv = scale.value() lbl_result0.value('{:6.2f}'.format(sv)) self.scale1.value( scale.value()) # Cause lower scale to mimic this one self.scale0 = ScaleLog((0, 30), legendcb=legendcb, value=15, cb_move=scale_move0, **scales) # Scale 1 with varying color. Label((0, 130), font=font14, value='Varying colors, follows top scale.') lbl_result1 = Label((200, 240), **labels) # Define callbacks for scale 1 def tickcb(f, c): if f > 30000: return RED if f < 10: return BLUE return c def scale_move1(scale): sv = scale.value() lbl_result1.value('{:6.2f}'.format(sv)) self.scale1 = ScaleLog((0, 160), tickcb=tickcb, cb_move=scale_move1, **scales) # Define buttons x = 390 y = 242 backbutton(x, y) y -= 50 Button((x, y), fgcolor=GREEN, text='Enable', callback=self.en, **buttons) y -= 50 Button((x, y), fgcolor=YELLOW, text='Disable', callback=self.dis, **buttons) y -= 50 Button((x, y), fgcolor=BLUE, text='Reset', callback=lambda _: self.scale1.value(1), **buttons)
def __init__(self): super().__init__() Button((390, 240), font=IFONT16, callback=self.quit, fgcolor=RED, text='Quit', shape=RECTANGLE, width=80, height=30) dial = VectorDial((120, 0), fgcolor=YELLOW, border=2) hrs = Pointer(dial) mins = Pointer(dial) k0 = Knob((0, 0), fgcolor=GREEN, bgcolor=(0, 0, 80), color=(168, 63, 63), border=2, cb_end=self.callback, cbe_args=['Knob1'], cb_move=self.knob_moved, cbm_args=(mins, 0.9)) k1 = Knob((0, 120), fgcolor=WHITE, border=2, arc=pi * 1.5, cb_end=self.callback, cbe_args=['Knob2'], cb_move=self.knob_moved, cbm_args=(hrs, 0.7)) Label((0, 225), font=font10, value='Arc = 270 degs.') # Dropdown self.lbl_dd = Label((120, 120), font=IFONT16, width=100, border=2, bgcolor=(0, 40, 0), fgcolor=RED) self.dropdown = Dropdown( (280, 0), font=IFONT16, width=100, callback=self.cbdb, elements=('Dog', 'Cat', 'Rat', 'Goat', 'Snake', 'Pig')) btnrst = Button((280, 70), font=IFONT16, callback=self.set_dropdown, fgcolor=BLUE, text='Reset', shape=RECTANGLE, width=80, height=30) # Test of set by value btnsnake = Button((280, 120), font=IFONT16, callback=self.set_bytext, args=('Snake', ), fgcolor=CYAN, fontcolor=BLACK, text='Snake', shape=RECTANGLE, width=80, height=30) # test set by text # Listbox self.lbl_lb = Label((120, 150), font=IFONT16, width=100, border=2, bgcolor=(0, 40, 0), fgcolor=RED) listbox = Listbox( (370, 70), font=IFONT16, width=105, bgcolor=GREY, fgcolor=YELLOW, select_color=BLUE, elements=('aardvark', 'zebra', 'armadillo', 'warthog'), callback=self.cblb) # On/Off toggle grey style self.lbl_style = Label((170, 210), font=font10, value='Current style: grey') bstyle = ButtonList(self.cb_style) bstyle.add_button((170, 240), font=IFONT16, fontcolor=WHITE, height=30, width=90, fgcolor=RED, shape=RECTANGLE, text='Dim', args=(False, )) bstyle.add_button((170, 240), font=IFONT16, fontcolor=WHITE, height=30, width=90, fgcolor=GREEN, shape=RECTANGLE, text='Grey', args=(True, )) # On/Off toggle enable/disable bs = ButtonList(self.cb_en_dis) self.lst_en_dis = (bstyle, k0, k1, self.dropdown, listbox, btnrst, btnsnake) bs.add_button((280, 240), font=IFONT16, fontcolor=BLACK, height=30, width=90, fgcolor=GREEN, shape=RECTANGLE, text='Disable', args=(True, )) bs.add_button((280, 240), font=IFONT16, fontcolor=BLACK, height=30, width=90, fgcolor=RED, shape=RECTANGLE, text='Enable', args=(False, ))
def backbutton(x, y): def back(button): Screen.back() Button((x, y), callback=back, fgcolor=CYAN, text='Back', **buttons)
def quitbutton(x, y): def quit(button): Screen.shutdown() Button((x, y), callback=quit, fgcolor=RED, text='Quit', **buttons)
def __init__(self): super().__init__() # These tables contain args that differ between members of a set of related buttons table = [ { 'fgcolor': GREEN, 'text': 'Yes', 'args': ('Oui', 2), 'fontcolor': (0, 0, 0) }, { 'fgcolor': RED, 'text': 'No', 'args': ('Non', 2) }, { 'fgcolor': BLUE, 'text': '???', 'args': ('Que?', 2) }, # 'fill': False to see effect { 'fgcolor': GREY, 'text': 'Rats', 'args': ('Rats', 2) }, ] # Highlight buttons: only tabulate data that varies table_highlight = [ { 'text': 'P', 'args': ('p', 2) }, { 'text': 'Q', 'args': ('q', 2) }, { 'text': 'R', 'args': ('r', 2) }, { 'text': 'S', 'args': ('s', 2) }, ] # A Buttonset with two entries table_buttonset = [ { 'fgcolor': GREEN, 'shape': CLIPPED_RECT, 'text': 'Start', 'args': ('Live', 2) }, { 'fgcolor': RED, 'shape': CLIPPED_RECT, 'text': 'Stop', 'args': ('Die', 2) }, ] table_radiobuttons = [ { 'text': '1', 'args': ('1', 3) }, { 'text': '2', 'args': ('2', 3) }, { 'text': '3', 'args': ('3', 3) }, { 'text': '4', 'args': ('4', 3) }, ] labels = { 'width': 70, 'fontcolor': WHITE, 'border': 2, 'fgcolor': RED, 'bgcolor': (0, 40, 0), 'font': font14, } # Uncomment this line to see 'skeleton' style greying-out: # Screen.tft.grey_color() # Labels self.lstlbl = [] for n in range(5): self.lstlbl.append(Label((390, 40 * n), **labels)) self.lst_en_dis = [] # Controls affected by disable button. # Button assortment x = 0 for t in table: self.lst_en_dis.append( Button((x, 0), font=font14, shape=CIRCLE, callback=self.callback, **t)) x += 70 # Highlighting buttons x = 0 for t in table_highlight: self.lst_en_dis.append( Button((x, 60), fgcolor=CYAN, shape=CIRCLE, fontcolor=BLACK, litcolor=WHITE, font=font14, callback=self.callback, **t)) x += 70 # Pad lbl_pad = Label((0, 200), value='touch me', fontcolor=WHITE, border=2, fgcolor=RED, bgcolor=DARKGREEN, font=font14) pad = Pad((0, 200), width=lbl_pad.width, height=lbl_pad.height, onrelease=False, callback=lambda _: lbl_pad.value('Short'), lp_callback=lambda _: lbl_pad.value('Long')) self.lst_en_dis.append(pad) # Start/Stop toggle self.bs = ButtonList(self.callback) self.bs0 = None for t in table_buttonset: # Buttons overlay each other at same location button = self.bs.add_button((0, 240), font=font14, fontcolor=BLACK, height=30, **t) if self.bs0 is None: # Save for reset button callback self.bs0 = button self.lst_en_dis.append(self.bs) # Radio buttons x = 0 self.rb = RadioButtons(BLUE, self.callback) # color of selected button self.rb0 = None for t in table_radiobuttons: button = self.rb.add_button((x, 140), font=font14, fontcolor=WHITE, fgcolor=(0, 0, 90), height=40, width=40, **t) if self.rb0 is None: # Save for reset button callback self.rb0 = button x += 60 self.lst_en_dis.append(self.rb) # Checkbox self.cb1 = Checkbox((340, 0), callback=self.cbcb, args=(0, )) self.cb2 = Checkbox((340, 40), fillcolor=RED, callback=self.cbcb, args=(1, )) self.lst_en_dis.extend([self.cb1, self.cb2]) # Reset button self.lbl_reset = Label((200, 210), font=font10, value='Reset also responds to long press') self.btn_reset = Button((300, 240), font=font14, height=30, width=80, fgcolor=BLUE, shape=RECTANGLE, text='Reset', fill=True, callback=self.cbreset, args=(4, ), onrelease=False, lp_callback=self.callback, lp_args=('long', 4)) # Quit self.btn_quit = Button((390, 240), font=font14, height=30, width=80, fgcolor=RED, shape=RECTANGLE, text='Quit', callback=self.quit) # Enable/Disable toggle self.bs_en = ButtonList(self.cb_en_dis) self.bs_en.add_button((200, 240), font=font14, fontcolor=BLACK, height=30, width=90, fgcolor=GREEN, shape=RECTANGLE, text='Disable', args=(True, )) self.bs_en.add_button((200, 240), font=font14, fontcolor=BLACK, height=30, width=90, fgcolor=RED, shape=RECTANGLE, text='Enable', args=(False, ))