The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Python: Multiple apps in one Python app - here is How to!
01-30-2022, 10:06 PM (This post was last modified: 01-30-2022 10:53 PM by Guenter Schink.)
Post: #1
Python: Multiple apps in one Python app - here is How to!
Edit: added Error handling in main.py

The limitation of the PYTHON app that it is apparently not possible to have multiple applications without creating ever new Python apps is really inconvenient.

I've now created an attempt to overcome this limitation. To follow up, I'd suggest to first create another Python app by saving the original to e.g. 'Catalog'. There shouldn't be anything in it except for an empty 'main.py'. The demo consists of 4 files. That should be sufficient to show how it works.
Step 1: copy this program into 'main.py'
Code:
from hpprime import *
     
def string(arg):return '"' + str(arg) +'"'
def strip(arg):return str(arg)[1:-1]
if 'Error' in eval('AVars("run")'): #edited
    eval('AVars("run"):="123"')

apps=[string("Choose APP")]

apps.append(string("Square"))
apps.append(string("Triangle"))
apps.append(string("Circle"))
#apps.append(string("none"))

n=int(eval('CHOOSE(N,'+strip(apps)+')'))
eval('run:='+apps[n])
'main.py' provides the 'CHOOSE' function from the HOME environment.
The Items to chose from are in that 'apps.append(string(("Square"))' for example. Here we have three items to select from: 'Square', 'Triangle' and 'Circle'. The 'string()' and 'strip()' functions make the 'CHOOSE()' function readable for the HOME environment.

To add another item simply copy one of these apps.app... lines and insert a string, preferable the filename you later wish to call.

Now 3 little programs for demonstration.
In the 'Catalog' app create a new file:'Square' and copy this program into it
Code:
from graphic import *
from hpprime import *
from urandom import randint
from sys import exit
if eval('run')!="Square":exit()
 
def Square():
 x=randint(0,320)-30
 y=randint(0,240)-30
 Size=randint(30,80)
 red=randint(0,255)
 green=randint(0,255)
 blue=randint(0,255)
 color=((red*256)+green)*256+blue
 draw_polygon([(x,y),(x+Size,y),(x+Size,y+Size),(x,y+Size)],color)
 

fillrect(0,0,0,320,240,white,white)
for i in range(100):
   Square()
   eval('WAIT(0.05)')
eval('WAIT')

Now create a file 'Triangle' and copy this program into it.
Code:
from graphic import *
from hpprime import *
from urandom import randint
from sys import exit
if eval('run')!="Triangle":exit()
 
def Triangle():
 x1,y1=(randint(0,350)-30,randint(0,270)-30)
 x2,y2=(randint(0,350)-30,randint(0,270)-30)
 x3,y3=(randint(0,350)-30,randint(0,270)-30)
 Size=randint(30,80)
 red=randint(0,255)
 green=randint(0,255)
 blue=randint(0,255)
 color=((red*256)+green)*256+blue
 draw_polygon([(x1,y1),(x2,y2),(x3,y3)],color)
 
fillrect(0,0,0,320,240,white,white)
for i in range(30):
   Triangle()
   eval('WAIT(0.05)')
eval('WAIT')
Finally create 'Circle.py' and copy this program into it.
Code:
from graphic import *
from hpprime import *
from urandom import *
from sys import exit
if eval('run')!="Circle":exit()

 
def Circle1():
 x=randint(0,350)-30
 y=randint(30,270)-30
 Width=randint(30,50)
 Height=randint(30,50) 
 red=randint(0,255)
 green=randint(0,255)
 blue=randint(0,255)
 color=((red*256)+green)*256+blue
 draw_arc(x,y,Width,Height,0,180,color)
 

fillrect(0,0,0,320,240,white,white)
for i in range(50):
   Circle1()
   eval('WAIT(0.05)')
eval('WAIT')

Now How does it work! The mandatory lines in each file are
Code:
from hpprime import *
from sys import exit
if eval('run')!="Circle":exit()
'hpprime' is necessary for the 'eval()' function
'from sys import exit' is necessary for the 'exit()' function
In the line:
'if eval('run')!="Circle":exit()' the string - here "Circle' must match the respective string in 'main.py'

All these little programs would run one after another were there not the test if the corresponding string is stored in the AVar "run". If not the program immediately terminates by the 'exit()' function.

Upon activating 'Catalog' you'll be presented with a CHOOSE menu, and after making your choice the corresponding program will run.

Caveat: It doesn't do it always - but that's not a real problem. I don't really know what's going on. But my observation is: Only programs that are listed before 'main.py' are executed right away after selection. The other ones need the [Clear] button to be touched - et voilà.

On my G2 this behavior disappeared after keying [On]+[Symb]. the main.py then was the last on, shrug.. If any one knows more about this please tell us.

I think best practice would be to develop a program separately and once it's finished, copy it into this 'Catalog'.

Perhaps someone likes it. comments , questions are welcome of course Smile

Günter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Python: Multiple apps in one Python app - here is How to! - Guenter Schink - 01-30-2022 10:06 PM



User(s) browsing this thread: 1 Guest(s)