micropython-micro-gui/gui/demos/adjuster.py

59 wiersze
1.9 KiB
Python
Czysty Zwykły widok Historia

2021-11-08 11:35:06 +00:00
# adjuster.py Demo of Adjusters linked to Labels
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2021 Peter Hinch
# hardware_setup must be imported before other modules because of RAM use.
import hardware_setup # Create a display instance
from gui.core.ugui import Screen, ssd
from gui.widgets.label import Label
from gui.widgets.buttons import CloseButton
from gui.widgets.adjuster import Adjuster
from gui.core.writer import CWriter
# Font for CWriter
import gui.fonts.font10 as font
from gui.core.colors import *
class BaseScreen(Screen):
def __init__(self):
super().__init__()
wri = CWriter(ssd, font, GREEN, BLACK)
col = 2
row = 2
self.lbl1 = Label(wri, row, col, 60, bdcolor=RED)
a = Adjuster(wri, row, self.lbl1.mcol + 2, fgcolor=RED, callback=self.adj1_callback)
Label(wri, row, a.mcol + 2, "Simple")
row = self.lbl1.mrow + 5
self.lbl2 = Label(wri, row, col, 60, bdcolor=RED)
a =Adjuster(wri, row, self.lbl2.mcol + 2, fgcolor=RED, value=0.5, callback=self.adj2_callback)
Label(wri, row, a.mcol + 2, "Scale")
row = self.lbl2.mrow + 5
self.lbl3 = Label(wri, row, col, 60, bdcolor=YELLOW)
a = Adjuster(wri, row, self.lbl3.mcol + 2, fgcolor=YELLOW, callback=self.adj3_callback)
Label(wri, row, a.mcol + 2, "Log")
CloseButton(wri) # Quit the application
def adj1_callback(self, adj):
v = adj.value() # Typically do mapping here
self.lbl1.value(f'{v:4.2f}')
def adj2_callback(self, adj):
v = (adj.value() - 0.5) * 10 # Scale and offset
self.lbl2.value(f'{v:4.2f}')
def adj3_callback(self, adj):
v = 10 ** (3 * adj.value()) # Log 3 decades
self.lbl3.value(f'{v:4.2f}')
def test():
print('Demo of Adjuster control.')
Screen.change(BaseScreen) # A class is passed here, not an instance.
test()