User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Tue Apr 24, 2012 4:34 pm

The following is a relatively simple python program loaded with lots of comments for the individual who's not very advanced in programming

I can confirm that the program works bug free, but I also recognize that there are *tons* of other ways this could have been done so if your only comment is that I should have used Popen instead of os please just skip the comment altogether but if there's a big legit way to improve this or a glaring error please let me know!

Improvements I know could be made:

1. Better reuse of code

2. Dynamic building of menus based on passed values and array sizes (see# 1)

Improvements I'd like to eventually make

1. Make it easier for users to edit (key combination or special key takes you to menu builder, you input your desired entries, and the command for them to run (New menu, exit, up level, launch program)

Before I post the code, here"s a simple screen shot I took yesterday before adding submenus (or an exit) just to give you a general idea of the design



 Here's a link to the code.  Many thanks to @Mjiig for reminding me about github!  I'll update the code as it develops, but it's very much a working scripts as is.  Anything I do to it from here on in will just be behind the scenes improvement and considering how basic a program it is, rather superfluous!

[EDIT: 05/22/2013] Removed raw dump of code, check Gist for the code https://gist.github.com/abishur/2482046
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Tue Apr 24, 2012 4:39 pm

P.S. This was my first phyton program and if you're in the same boat there's some things you should know about python

1. space matter.  Rather than having a return or exit at the end of your code, they use spaces to keep track of when your code ends or when a while statement or if statement or etc ends

2. No semicolons, almost a decade of automatically putting a ; made for some interesting typing

3. Functions are declared before you use them.  I'm used to putting my functions at the end of my program, but if you do that the program will fail.  A function must be defined prior to calling them
Dear forum: Play nice ;-)

Mjiig
Posts: 21
Joined: Mon Dec 05, 2011 8:44 pm

Re: Simple Terminal (framebuffer) menu system in python

Tue Apr 24, 2012 5:31 pm

I created a gist with the code in for anyone who wants to take a look at it with syntax highlighting who doesn't have a python IDE to hand.

Python did complain about some bad indenting which I assume is a casualty of getting copy and pasted (possibly some tabs mixed in with spaces or vice versa that got converted badly), so I fixed those in the gist. Quite a nice piece of code there though, and well commented (I actually managed to understand it despite being a complete python novice).

Thanks

Angus

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Tue Apr 24, 2012 5:36 pm

Thanks for putting it up Angus!  I knew there had to be some place like that!  Yeah for some reason it just doesn't like it when I open things up in notepad++, but it's the only editor I have on the windows PC right now that preserves the information anywhere near correctly!
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Tue Apr 24, 2012 5:41 pm

Doh!  It turned out I already had a github account where I could have done what you did for me!  Is there anyway you can add me (abishur) to the list of approved editors for that page so I can make updates?  I know I could just put up my own page, but you were nice enough to put it up for me
Dear forum: Play nice ;-)

Mjiig
Posts: 21
Joined: Mon Dec 05, 2011 8:44 pm

Re: Simple Terminal (framebuffer) menu system in python

Tue Apr 24, 2012 5:51 pm

Abishur said:


Doh!  It turned out I already had a github account where I could have done what you did for me!  Is there anyway you can add me (abishur) to the list of approved editors for that page so I can make updates?  I know I could just put up my own page, but you were nice enough to put it up for me


I don't have a github account at all! The gist is anonymous so I don't think I can edit it anymore than you can. You can always fork it because gists are actually proper git repositories, but I appreciate that's not ideal.

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Tue Apr 24, 2012 6:14 pm

Edited first post to link to the code on my github login where I can make changes as needed
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Wed Apr 25, 2012 2:15 am

Re-edited the first post as I realized some people might want to actually quote it in their replies
Dear forum: Play nice ;-)

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Thu Apr 26, 2012 1:12 am

As an experienced programmer, seeing duplicated data (like the way you list every menu item twice) and/or hardcoded values makes me cringe

So I've updated your program so the the entire menu structure is only specified once, and you can have as many sub-menus, sub-sub-menus etc. as you want without having to update any numbers anywhere

123#!/usr/bin/env python# -*- coding: utf-8 -*-# Topmenu and the submenus are based of the example found at this location http://blog.skeltonnetworks.com/2010/03 ... stom-menu/

I've tried not to make too many changes to your code in the hope that it's still understandable! Once you're more familiar with Python I'd recommend changing the fragile nested menu_data object with a set of classes/objects. And then you might want to have it read the menu definition from an external datafile. And then...

Even if you choose to ignore my code, there's still a bug in your current version - if you enter the DOS Games submenu, and then go back to the top menu, you can't then re-enter the DOS submenu. The fix is to set "sub1get = None" before you do "while sub1get !=ord('3'):"

I don't have a RaspberryPi yet, so I've only tested my code on a regular Linux PC (but of course there's no reason it shouldn't run on a Pi).

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Thu Apr 26, 2012 1:16 am

Huh?! Dunno what the forum did there! I tried to post a link to

Edit: Argh it did it again.





123



#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Topmenu and the submenus are based of the example found at this location http://blog.skeltonnetworks.co.....stom-menu/






Edit2: Argh, WTF?!

http://gist.github.com/ and then add 2494781

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Thu Apr 26, 2012 1:52 pm

Thanks for taking care of issues #1 and #2, Andrew!
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Thu Apr 26, 2012 2:18 pm

Andrew Scheller said:


Even if you choose to ignore my code, there's still a bug in your current version – if you enter the DOS Games submenu, and then go back to the top menu, you can't then re-enter the DOS submenu. The fix is to set "sub1get = None" before you do "while sub1get !=ord('3'):"


Oh wow, I can't believe I missed that!  Thanks again!
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Thu Apr 26, 2012 2:35 pm

Also, right after the curses.endwin() I've added an os.system('clear').  This clears the screen and leaves you with a nice and neat bash prompt.  More of an ascetics thing than anything else
Dear forum: Play nice ;-)

User avatar
jbeale
Posts: 3439
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Simple Terminal (framebuffer) menu system in python

Thu Apr 26, 2012 4:47 pm

Your meaning is clear, but just because I'm obsessive...

ascetic: a person who dedicates his or her life to a pursuit of contemplative ideals and practices extreme self-denial or self-mortification for religious reasons.

aesthetic: concerned with beauty, artistic impact, or appearance.

Another minor gripe: due to the way this page appears on my phone, your signature takes several pages to scroll through. Any chance of it being a bit shorter?

Return to “Python”