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 * 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 * Now create a file 'Triangle' and copy this program into it. Code: from graphic import * Code: from graphic import * Now How does it work! The mandatory lines in each file are Code: from hpprime import * '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 Günter |
|||
10-25-2022, 08:32 PM
Post: #2
|
|||
|
|||
RE: Python: Multiple apps in one Python app - here is How to!
Quote: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.Main.py renames itself randomly whenever I create a new file (be it binary or python program) Very good writeup; On my G1 it also happens So I don't for now resort to making python applications. HP Prime G1 Python via Android Termux... |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)