kopia lustrzana https://github.com/peterhinch/micropython-micro-gui
FloatAdj uses inheritance not composition.
rodzic
5c7d6c96b3
commit
5f4369fbb3
|
@ -39,9 +39,10 @@ class BaseScreen(Screen):
|
||||||
a = Adjuster(wri, row, self.lbl3.mcol + 2, fgcolor=YELLOW,
|
a = Adjuster(wri, row, self.lbl3.mcol + 2, fgcolor=YELLOW,
|
||||||
callback=self.adj3_callback)
|
callback=self.adj3_callback)
|
||||||
Label(wri, row, a.mcol + 2, "Log")
|
Label(wri, row, a.mcol + 2, "Log")
|
||||||
|
# Demo of FloatAdj class
|
||||||
self.fa = FloatAdj(wri, a.mrow + 5, col, color=BLUE,
|
self.fa = FloatAdj(wri, a.mrow + 5, col, color=BLUE,
|
||||||
map_func=lambda x: (x - 0.5) * 20,
|
map_func=lambda x: (x - 0.5) * 20,
|
||||||
fstr="{:6.2f}", text="class")
|
value=0.5, fstr="{:6.2f}", text="class")
|
||||||
CloseButton(wri) # Quit the application
|
CloseButton(wri) # Quit the application
|
||||||
|
|
||||||
def adj1_callback(self, adj):
|
def adj1_callback(self, adj):
|
||||||
|
@ -57,7 +58,7 @@ class BaseScreen(Screen):
|
||||||
self.lbl3.value(f"{v:4.2f}")
|
self.lbl3.value(f"{v:4.2f}")
|
||||||
|
|
||||||
#def after_open(self): # Demo of programmatic change
|
#def after_open(self): # Demo of programmatic change
|
||||||
#self.fa.value(0.5)
|
#self.fa.value(0.8)
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
print("Demo of Adjuster control.")
|
print("Demo of Adjuster control.")
|
||||||
|
|
|
@ -52,37 +52,33 @@ class Adjuster(LinearIO):
|
||||||
display.line(int(self.xorigin), int(self.yorigin), x_end, y_end, color)
|
display.line(int(self.xorigin), int(self.yorigin), x_end, y_end, color)
|
||||||
|
|
||||||
# This class combines an Adjuster with one or two labels. Numerous layout
|
# This class combines an Adjuster with one or two labels. Numerous layout
|
||||||
# options exist: users may wish to write their own versions of this example.
|
# options exist: users may wish to write a version of this example with
|
||||||
# The map_func enables instances to have their own mapping of Adjuster value
|
# different visual characteristics.
|
||||||
|
# The map_func enables each instance to have its own mapping of Adjuster value
|
||||||
# to perform offset, scaling, log mapping etc.
|
# to perform offset, scaling, log mapping etc.
|
||||||
# The object's value is that of the Adjuster, in range 0.0-1.0. The scaled
|
# The object's value is that of the Adjuster, in range 0.0-1.0. The scaled
|
||||||
# value is retrieved with .mapped_value()
|
# value is retrieved with .mapped_value()
|
||||||
class FloatAdj:
|
class FloatAdj(Adjuster):
|
||||||
def __init__(self, wri, row, col, *,
|
def __init__(self, wri, row, col, *,
|
||||||
lbl_width=60, value=0.0, color=None,
|
lbl_width=60, value=0.0, color=None,
|
||||||
fstr="{:4.2f}", map_func=lambda v: v, text="",
|
fstr="{:4.2f}", map_func=lambda v: v, text="",
|
||||||
callback=dolittle, args=[]):
|
callback=dolittle, args=[]):
|
||||||
|
|
||||||
self.fstr = fstr
|
|
||||||
self.map_func = map_func
|
self.map_func = map_func
|
||||||
self.callback = callback
|
self.facb = callback
|
||||||
self.args = args
|
self.args = args
|
||||||
|
self.fstr = fstr
|
||||||
|
|
||||||
self.lbl = Label(wri, row, col, lbl_width, bdcolor=color)
|
self.lbl = Label(wri, row, col, lbl_width, bdcolor=color)
|
||||||
self.adj = Adjuster(wri, row, self.lbl.mcol + 2, value=value,
|
super().__init__(wri, row, self.lbl.mcol + 2, value=value,
|
||||||
fgcolor=color, callback=self.cb)
|
fgcolor=color, callback=self.cb)
|
||||||
l = Label(wri, row, self.adj.mcol + 2, text) if text else self.adj
|
if text:
|
||||||
# Facilitate relative positioning.
|
self.legend = Label(wri, row, self.mcol + 2, text)
|
||||||
self.mcol = l.mcol
|
self.mcol = self.legend.mcol # For relative positioning
|
||||||
self.mrow = self.adj.mrow
|
|
||||||
|
|
||||||
def cb(self, adj):
|
def cb(self, adj): # Runs on init and when value changes
|
||||||
self.lbl.value(self.fstr.format(self.mapped_value(adj)))
|
self.lbl.value(self.fstr.format(self.mapped_value()))
|
||||||
self.callback(self, *self.args)
|
self.facb(self, *self.args) # Run user callback
|
||||||
|
|
||||||
# Behave like a Widget.
|
def mapped_value(self):
|
||||||
def value(self, v=None):
|
return self.map_func(self.value())
|
||||||
return self.adj.value(v)
|
|
||||||
|
|
||||||
def mapped_value(self, adj=None): # Special handling for initial callback
|
|
||||||
return self.map_func(self.value() if adj is None else adj.value())
|
|
||||||
|
|
Ładowanie…
Reference in New Issue