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

75 wiersze
2.1 KiB
Python
Czysty Zwykły widok Historia

2021-06-23 17:05:14 +00:00
# screen_change.py Minimal micro-gui demo showing a Button causing a screen change.
# Released under the MIT License (MIT). See LICENSE.
2025-03-23 12:49:50 +00:00
# Copyright (c) 2021-2025 Peter Hinch
2021-06-23 17:05:14 +00:00
# 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
2022-02-06 12:05:38 +00:00
from gui.widgets import Button, CloseButton, Label
2021-06-23 17:05:14 +00:00
from gui.core.writer import CWriter
# Font for CWriter
2021-06-25 15:56:14 +00:00
import gui.fonts.arial10 as font
2021-06-23 17:05:14 +00:00
from gui.core.colors import *
2025-03-23 12:49:50 +00:00
import asyncio
import time
2021-06-23 17:05:14 +00:00
# Defining a button in this way enables it to be re-used on
# multiple Screen instances. Note that a Screen class is
# passed, not an instance.
2025-03-23 12:49:50 +00:00
def fwdbutton(wri, row, col, cls_screen, text="Next"):
2021-06-23 17:05:14 +00:00
def fwd(button):
Screen.change(cls_screen) # Callback
2025-03-23 12:49:50 +00:00
Button(wri, row, col, callback=fwd, fgcolor=BLACK, bgcolor=GREEN, text=text, shape=RECTANGLE)
2021-06-23 17:05:14 +00:00
2021-06-25 15:56:14 +00:00
wri = CWriter(ssd, font, GREEN, BLACK, verbose=False)
2021-06-23 17:05:14 +00:00
# This screen overlays BaseScreen.
class BackScreen(Screen):
def __init__(self):
super().__init__()
2025-03-23 12:49:50 +00:00
Label(wri, 2, 2, "New screen.")
2021-06-23 17:05:14 +00:00
CloseButton(wri)
2025-03-23 12:49:50 +00:00
# The splash screen is a special case of a Screen with no active widgets.
class SplashScreen(Screen):
def __init__(self):
super().__init__(wri) # No sctive widgets: must pass writer
Label(wri, 10, 10, "Splash screen")
Label(wri, 40, 10, "Please wait", fgcolor=YELLOW)
def after_open(self): # Cle after a period
asyncio.create_task(self.close_me())
2021-06-23 17:05:14 +00:00
2025-03-23 12:49:50 +00:00
async def close_me(self):
await asyncio.sleep(3)
Screen.back()
class BaseScreen(Screen):
2021-06-23 17:05:14 +00:00
def __init__(self):
super().__init__()
2025-03-23 12:49:50 +00:00
Label(wri, 2, 2, "Base screen.")
2021-06-23 17:05:14 +00:00
fwdbutton(wri, 40, 2, BackScreen)
CloseButton(wri)
2025-03-23 12:49:50 +00:00
self.splash_done = False
def after_open(self):
if not self.splash_done: # Only show splash on initial opening.
Screen.change(SplashScreen)
self.splash_done = True
2021-06-23 17:05:14 +00:00
def test():
2025-03-23 12:49:50 +00:00
print("Screen change demo.")
2021-06-23 17:05:14 +00:00
Screen.change(BaseScreen) # Pass class, not instance!
2025-03-23 12:49:50 +00:00
2021-06-23 17:05:14 +00:00
test()