Menu

Complex Main Example

Benedict Jäggi

Complex MAIN.py Example

Back to [MAIN] (Simple example)

import APP_BASE # This one is mandatory
import UITools  # This is used to show an example message here.

####### APPLICATION RELATED STUFF ########

# import the face app as system app.
import APP_Face
# Best is if you give your app the same name like the corresponding file name.
# Each App-file has a class called App (if you do not rename it.)
# You need to give an instance of the App as parameter.
# Do that at the end of your Application File. It's just here for reference.
APP_BASE.registerApplication("APP_Face", APP_Face.App())

# import the menu app as "real" system app.
import APP_MENU

# And just for fun, import a game app.
import APP_GAME_Breakout

######## DONE IMPORTING ########

# Then set the current running application:
# We show the robot face on startup.
APP_BASE.setCurrentApp("APP_Face")`

# And set the underlying ("system") App, which gets  called when you "exit" another app:
# It will be APP_Face here but it could also be APP_MENU for example.
APP_BASE.underlyingApp = "APP_Face"`

# APP_Face also has an underlying app which is called when you touch 
# the lower part of the display. Call APP_MENU then.
APP_Face.underlyingApp = "APP_MENU"

######## MENU RELATED STUFF ##########

# Our menu App does need some functional buttons. 
# Let's set them up.
# First create a menu.
# You don't have to but it's good practice.
# (A menu instance will be created on startup, which you can affect directly.)
# A menu has a default of 3x2=6 buttons if not otherwise specified. See below.
mainMenu = APP_MENU.cMenu()

# another menu to switch to for example
# here, we set how many buttons there are: 2 buttons (x) on 2 lines (y) = 4 buttons.
anotherMenu = APP_MENU.cMenu(2,2)

# Then set up the menu functions.

####### MENU FUNCTIONS #######

# Just show a message.
def menuShowMsgFunc():
    # last flag determines if the message will be hidden on touch (True) 
    # or release (False) on the display. Default is release (mouseup).
     UITools.showMessage("This is the middle text.", "Upper text.", "Lower text.", False)

# "Exit" the menu
def menuExitFunc():
    APP_BASE.setCurrentApp(APP_BASE.underlyingApp)

# switch to the breakout game
def menuGameBreakoutFunc():
     APP_BASE.setCurrentApp("APP_GAME_Breakout")

 # select another menu on touch.
def menuSwitchFunc():
    global anotherMenu
    APP_MENU.CURRENT_MENU=anotherMenu
    # maybe we call that from another App than the menu, set the menu as current App then.
    # You don't need that if you are switching between menus in the MENU_APP, when it is the current running App.
    # It's just here for reference.
    APP_BASE.setCurrentApp("APP_MENU")

def menuSwitchBackFunc():
    global mainMenu
     APP_MENU.CURRENT_MENU=mainMenu
     # see comment above
     APP_BASE.setCurrentApp("APP_MENU")

###### MENU ITEM SETTINGS #######

# set some buttons on the menu:
# first is the index, then the text shown and finally the function to call.
# the upper 3 buttons have index 0 to 2, the lower 3 buttons have index 3 to 5
mainMenu.setMenuItem(0, "BREAKOUT", menuGameBreakoutFunc)
mainMenu.setMenuItem(1, "SHOW MSG", menuShowMsgFunc)

# set the switch back function to a button in anothermenu
# It's on the top right with index 1, index 0 is top left from 4 buttons, so index 2 and 3 are the lower line.
anotherMenu.setMenuItem(1, "<--", menuSwitchBackFunc)

###### SELECT MENU ######
# Select the main menu.
# If no menu is selected, it will use an internal one.
# But you can loose it when you select another menu!
# It's a safer way to create menu variables and select them.
APP_MENU.CURRENT_MENU = mainMenu

########## DIRECT MENU ITEM SETTING ############
# You can also set items in the CURRENT selected menu directly:
# Let's do that for the rightmost buttons in the main menu.
# Remember: The main menu has the default of 6 buttons but anotherMenu just has 4 buttons.
# Or otherwise: The lower right button in the main menu has index 5 (0-5= 6 buttons) 
# and in anotherMenu the same button has index 3 (0-3 = 4 buttons)

APP_MENU.setMenuItem(3, "-->", menuSwitchFunc)
APP_MENU.setMenuItem(5, "EXIT", menuExitFunc)

########## RUN THE SYSTEM ##############

# Finally, initialize the system and run it:
APP_BASE.initialize()
running = True
while running:
      running=APP_BASE.update()

APP_BASE.quit()

You should now see a face when you start MAIN.py.
If you click on the lower part, the MENU_APP shows up.
There should be 6 buttons which

  1. switches to Breakout, 2. shows a Message, 3. switches to another menu, 4 and 5 do nothing and 6 returns to the face app when touched.
    When you switch to the other menu with "-->", there should only be 4 buttons, where one can switch back ("<--") and the others do nothing.

Now you learned how to switch between apps with the MENU_APP, the Face App and the Breakout game App.


Learn how to create one of these shiny Apps for yourself with the [APP_TEMPLATE].

Or go back [Home]


Related

Wiki: APP_TEMPLATE
Wiki: Home
Wiki: MAIN
Wiki: UITools

MongoDB Logo MongoDB