kopia lustrzana https://github.com/peterhinch/micropython-micro-gui
Improve Button appearance. Fix Label bug where value too long.
rodzic
b4960b2941
commit
9fcdba8dbf
16
README.md
16
README.md
|
@ -53,11 +53,15 @@ Code is new and issues are likely: please report any found. This document is
|
||||||
under review. I plan to add further demos and to upgrade the performance of
|
under review. I plan to add further demos and to upgrade the performance of
|
||||||
some display drivers.
|
some display drivers.
|
||||||
|
|
||||||
|
An issue under investigation is that a soft reset is required after a GUI
|
||||||
|
application is run and before running another. Otherwise the second application
|
||||||
|
displays correctly but is unresponsive.
|
||||||
|
|
||||||
# 0. Contents
|
# 0. Contents
|
||||||
|
|
||||||
1. [Basic concepts](./README.md#1-basic-concepts) Including installation and test.
|
1. [Basic concepts](./README.md#1-basic-concepts) Including installation and test.
|
||||||
1.1 [Coordinates](./README.md#11-coordinates) The GUI's coordinate system.
|
1.1 [Coordinates](./README.md#11-coordinates) The GUI's coordinate system.
|
||||||
1.2 [Screen Window and Widget objects](./README.md#12-Screen-window-and-widget-objects)
|
1.2 [Screen Window and Widget objects](./README.md#12-Screen-window-and-widget-objects) Basic GUI classes.
|
||||||
1.3 [Fonts](./README.md#13-fonts)
|
1.3 [Fonts](./README.md#13-fonts)
|
||||||
1.4 [Navigation](./README.md#14-navigation) How the GUI navigates between widgets.
|
1.4 [Navigation](./README.md#14-navigation) How the GUI navigates between widgets.
|
||||||
1.5 [Hardware definition](./README.md#15-hardware-definition) How to configure your hardware.
|
1.5 [Hardware definition](./README.md#15-hardware-definition) How to configure your hardware.
|
||||||
|
@ -396,9 +400,11 @@ minimal and aim to demonstrate a single technique.
|
||||||
* `slider.py` A `Slider` whose color varies with its value.
|
* `slider.py` A `Slider` whose color varies with its value.
|
||||||
* `slider_label.py` A `Slider` updating a `Label`. Good for trying precision
|
* `slider_label.py` A `Slider` updating a `Label`. Good for trying precision
|
||||||
mode.
|
mode.
|
||||||
* `linked_slider.py` One slider updating two others, and a coding "wrinkle"
|
* `linked_slider.py` One `Slider` updating two others, and a coding "wrinkle"
|
||||||
required for doing this.
|
required for doing this.
|
||||||
* `screen_change.py` A `Pushbutton` causing a screen change.
|
* `dialog.py` `DialogBox` demo. Illustrates the screen change mechanism.
|
||||||
|
* `screen_change.py` A `Pushbutton` causing a screen change using a re-usable
|
||||||
|
"forward" button.
|
||||||
* `tbox.py` Text boxes and user-controlled scrolling.
|
* `tbox.py` Text boxes and user-controlled scrolling.
|
||||||
|
|
||||||
### 1.11.2 Test scripts
|
### 1.11.2 Test scripts
|
||||||
|
@ -643,10 +649,6 @@ In normal use the following methods only are required:
|
||||||
passing positional and keyword arguments to the constructor of the new, user
|
passing positional and keyword arguments to the constructor of the new, user
|
||||||
defined, screen.
|
defined, screen.
|
||||||
* `back(cls)` Restore previous screen.
|
* `back(cls)` Restore previous screen.
|
||||||
* `value(cls, val=None)` The `val` arg can be any Python type. It allows
|
|
||||||
widgets on a `Screen` to store information in a way which can be accessed from
|
|
||||||
the calling screen. This typically occurs after the screen has closed and no
|
|
||||||
longer exists as an instance, hence the use of a classmethod.
|
|
||||||
|
|
||||||
These are uncommon:__
|
These are uncommon:__
|
||||||
* `shutdown(cls)` Clear the screen and shut down the GUI. Normally done by a
|
* `shutdown(cls)` Clear the screen and shut down the GUI. Normally done by a
|
||||||
|
|
|
@ -202,15 +202,6 @@ class Display:
|
||||||
class Screen:
|
class Screen:
|
||||||
current_screen = None
|
current_screen = None
|
||||||
is_shutdown = Event()
|
is_shutdown = Event()
|
||||||
_value = None
|
|
||||||
|
|
||||||
# Allow a Screen to store an arbitrary object. Retrieval may be
|
|
||||||
# done by caller, after the Screen instance was deleted
|
|
||||||
@classmethod
|
|
||||||
def value(cls, val=None):
|
|
||||||
if val is not None:
|
|
||||||
cls._value = val
|
|
||||||
return cls._value
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def next_ctrl(cls):
|
def next_ctrl(cls):
|
||||||
|
@ -490,9 +481,19 @@ class Screen:
|
||||||
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
|
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
|
||||||
#print(gc.mem_free())
|
#print(gc.mem_free())
|
||||||
|
|
||||||
# Very basic window class. Cuts a rectangular hole in a screen on which content may be drawn
|
# Very basic window class. Cuts a rectangular hole in a screen on which
|
||||||
|
# content may be drawn.
|
||||||
class Window(Screen):
|
class Window(Screen):
|
||||||
|
|
||||||
|
_value = None
|
||||||
|
# Allow a Window to store an arbitrary object. Retrieval may be
|
||||||
|
# done by caller, after the Window instance was deleted
|
||||||
|
@classmethod
|
||||||
|
def value(cls, val=None):
|
||||||
|
if val is not None:
|
||||||
|
cls._value = val
|
||||||
|
return cls._value
|
||||||
|
|
||||||
def __init__(self, row, col, height, width, *, draw_border=True, bgcolor=None, fgcolor=None):
|
def __init__(self, row, col, height, width, *, draw_border=True, bgcolor=None, fgcolor=None):
|
||||||
Screen.__init__(self)
|
Screen.__init__(self)
|
||||||
self.row = row
|
self.row = row
|
||||||
|
|
|
@ -264,7 +264,7 @@ class Writer():
|
||||||
def setcolor(self, *_):
|
def setcolor(self, *_):
|
||||||
return self.fgcolor, self.bgcolor
|
return self.fgcolor, self.bgcolor
|
||||||
|
|
||||||
# Writer for colour displays or upside down rendering
|
# Writer for colour displays
|
||||||
class CWriter(Writer):
|
class CWriter(Writer):
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Button(Widget):
|
||||||
width = max(sl, height)
|
width = max(sl, height)
|
||||||
height = width
|
height = width
|
||||||
else:
|
else:
|
||||||
width = max(sl, width)
|
width = max(sl + 10, width) # Allow 5 pixels before and after text
|
||||||
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor, False, True)
|
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor, False, True)
|
||||||
self.shape = shape
|
self.shape = shape
|
||||||
self.radius = height // 2
|
self.radius = height // 2
|
||||||
|
@ -100,9 +100,9 @@ class Button(Widget):
|
||||||
class CloseButton(Button):
|
class CloseButton(Button):
|
||||||
def __init__(self, writer, width=0, callback=dolittle, args=(), bgcolor=RED):
|
def __init__(self, writer, width=0, callback=dolittle, args=(), bgcolor=RED):
|
||||||
scr = Screen.current_screen
|
scr = Screen.current_screen
|
||||||
# The factor of 2 is an empirical fix to make it look OK over
|
# Calculate the button width if not provided. Button allows
|
||||||
# the range of fonts in use.
|
# 5 pixels either side.
|
||||||
wd = width if width else writer.stringlen('X') * 2
|
wd = width if width else (writer.stringlen('X') + 10)
|
||||||
self.user_cb = callback
|
self.user_cb = callback
|
||||||
self.user_args = args
|
self.user_args = args
|
||||||
super().__init__(writer, *scr.locn(4, scr.width - wd - 4),
|
super().__init__(writer, *scr.locn(4, scr.width - wd - 4),
|
||||||
|
|
|
@ -25,7 +25,7 @@ class DialogBox(Window):
|
||||||
|
|
||||||
height = 80
|
height = 80
|
||||||
spacing = 10
|
spacing = 10
|
||||||
buttonwidth = max(max(writer.stringlen(e[0]) for e in elements) + 4, buttonwidth)
|
buttonwidth = max(max(writer.stringlen(e[0]) for e in elements) + 14, buttonwidth)
|
||||||
buttonheight = max(writer.height, 15)
|
buttonheight = max(writer.height, 15)
|
||||||
nelements = len(elements)
|
nelements = len(elements)
|
||||||
width = spacing + (buttonwidth + spacing) * nelements
|
width = spacing + (buttonwidth + spacing) * nelements
|
||||||
|
|
|
@ -9,6 +9,7 @@ from gui.core.colors import *
|
||||||
# text: str display string int save width
|
# text: str display string int save width
|
||||||
class Label(Widget):
|
class Label(Widget):
|
||||||
def __init__(self, writer, row, col, text, invert=False, fgcolor=None, bgcolor=BLACK, bdcolor=False):
|
def __init__(self, writer, row, col, text, invert=False, fgcolor=None, bgcolor=BLACK, bdcolor=False):
|
||||||
|
self.writer = writer
|
||||||
# Determine width of object
|
# Determine width of object
|
||||||
if isinstance(text, int):
|
if isinstance(text, int):
|
||||||
width = text
|
width = text
|
||||||
|
@ -22,6 +23,8 @@ class Label(Widget):
|
||||||
self.value(text, invert)
|
self.value(text, invert)
|
||||||
|
|
||||||
def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None):
|
def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None):
|
||||||
|
if self.writer.stringlen(text) > self.width:
|
||||||
|
raise ValueError('Label.value() string is too long.')
|
||||||
txt = super().value(text) # Sets .draw ensuring refresh
|
txt = super().value(text) # Sets .draw ensuring refresh
|
||||||
# Redraw even if no text supplied: colors may have changed.
|
# Redraw even if no text supplied: colors may have changed.
|
||||||
self.invert = invert
|
self.invert = invert
|
||||||
|
|
Ładowanie…
Reference in New Issue