Python Menu For Linux Mint 18.3

Go down

Python Menu For Linux Mint 18.3

Post by jamied_uk on 30th March 2018, 12:04

#!/usr/bin/env python

# Install all things needed for example:
# sudo apt install -y htop


# sudo chmod +x


# python


# Original concept

import pwd;

from time import sleep

import curses, os #curses is the interface for capturing key presses on the menu, os launches the files

screen = curses.initscr() #initializes a new window for capturing key presses

curses.noecho() # Disables automatic echoing of key presses (prevents program from input each key twice)

curses.cbreak() # Disables line buffering (runs each key as it is pressed rather than waiting for the return key to pressed)

curses.start_color() # Lets you use colors when highlighting selected menu option

screen.keypad(1) # Capture input from keypad


# Change this to use different colors when highlighting

curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_WHITE) # Sets up color pair #1, it does black text with white background

h = curses.color_pair(1) #h is the coloring for a highlighted menu option

n = curses.A_NORMAL #n is the coloring for a non highlighted menu option


MENU = "menu"

COMMAND = "command"

EXITMENU = "exitmenu"

userhome = os.path.expanduser('~')          

import commands

username = commands.getoutput("whoami");

menu_data = {

  'title': "J~Net Program Launcher Welcome Back " + username +" Home Folder "+ userhome , 'type': MENU, 'subtitle': "Please select an option...",


            { 'title': "Process List", 'type': COMMAND, 'command': 'htop' },

            { 'title': "Restart Apache Server", 'type': COMMAND, 'command': 'sudo /etc/init.d/apache2 restart' },

            { 'title': "Edit Alias's", 'type': COMMAND, 'command': 'sudo gedit /etc/bash.bashrc' },

          { 'title': "More Options", 'type': MENU, 'subtitle': "Please select an option...",

        'options': [

                   { 'title': "Crontab aka Cronjobs", 'type': COMMAND, 'command': 'sudo crontab -u root -e' },

        { 'title': "Firewall", 'type': COMMAND, 'command': 'sudo gufw' },{ 'title': "Connect Raspberry Pi", 'type': COMMAND, 'command': 'ssh' },

{ 'title': "System Update", 'type': COMMAND, 'command': 'sudo apt-get update' },

{ 'title': "Browse Network", 'type': COMMAND, 'command': 'sudo nmap -sS jnet-server' },

{ 'title': "Current IP", 'type': COMMAND, 'command': 'sudo ifconfig' },

{ 'title': "Net Check", 'type': COMMAND, 'command': 'timeout 5s ping' },


  { 'title': "More Options", 'type': MENU, 'subtitle': "Please select an option...",

        'options': [

                  { 'title': "Fix Audio Input", 'type': COMMAND, 'command': 'pkill jackd' },

       { 'title': "Close With Mouse", 'type': COMMAND, 'command': 'sudo xkill;' },

{ 'title': "List Out Running Apps", 'type': COMMAND, 'command': 'sudo  ps aux | less' },

{ 'title': "Gedit Text Editor", 'type': COMMAND, 'command': 'sudo gedit' },

{ 'title': "Alarm Clock", 'type': COMMAND, 'command': 'alarm-clock-applet' },

{ 'title': "Samba Shares", 'type': COMMAND, 'command': 'gksu system-config-samba' },

{ 'title': "Grub Customizer", 'type': COMMAND, 'command': 'grub-customizer' },

{ 'title': "More Options", 'type': MENU, 'subtitle': "Please select an option...",

        'options': [

                   { 'title': "User Administration", 'type': COMMAND, 'command': 'users-admin' },

      { 'title': "Open Openshot Editor", 'type': COMMAND, 'command': 'openshot %F' },

{ 'title': "System Monitor", 'type': COMMAND, 'command': 'ksysguard' },




        }, ]


      { 'title': "Games Menu", 'type': MENU, 'subtitle': "Please select an option...",

        'options': [

                   { 'title': "Dos Box", 'type': COMMAND, 'command': 'dosbox' },

 { 'title': "The Ur-Quan Masters", 'type': COMMAND, 'command': 'uqm' },

{ 'title': "Best Game Emulator", 'type': COMMAND, 'command': 'mupen64Plus' },

{ 'title': "Another Game Emulator", 'type': COMMAND, 'command': 'dolphin-emu' },

        { 'title': "XBMC", 'type': COMMAND, 'command': 'xbmc' },



        { 'title': "Login", 'type': COMMAND, 'command': 'python Auth/ Auth/1.txt' },

        { 'title': "Windows 3.1", 'type': COMMAND, 'command': 'dosbox /media/samba/Apps/dosbox/doswin/WINDOWS/WIN.COM -conf /home/pi/scripts/dosbox2.conf -exit' },

        { 'title': "Quick System Reboot", 'type': MENU, 'subtitle': "Select Yes to Reboot",

        'options': [

          {'title': "NO", 'type': EXITMENU, },

          {'title': "", 'type': COMMAND, 'command': '' },

          {'title': "", 'type': COMMAND, 'command': '' },

          {'title': "", 'type': COMMAND, 'command': '' },

          {'title': "YES", 'type': COMMAND, 'command': 'sudo shutdown -r -time now' },

          {'title': "", 'type': COMMAND, 'command': '' },

          {'title': "", 'type': COMMAND, 'command': '' },

          {'title': "", 'type': COMMAND, 'command': '' },






# This function displays the appropriate menu and returns the option selected

def runmenu(menu, parent):


  # work out what text to display as the last menu option

  if parent is None:

    lastoption = "Exit"


    lastoption = "Return to %s Menu" % parent['title']


  optioncount = len(menu['options']) # how many options in this menu


  pos=0 #pos is the zero-based index of the hightlighted menu option. Every time runmenu is called, position returns to 0, when runmenu ends the position is returned and tells the program what opt$

  oldpos=None # used to prevent the screen being redrawn every time

  x = None #control for while loop, let's you scroll through options until return key is pressed then returns pos to program


  # Loop until return key is pressed

  while x !=ord('\n'):

    if pos != oldpos:

      oldpos = pos


      screen.addstr(2,2, menu['title'], curses.A_STANDOUT) # Title for this menu

      screen.addstr(4,2, menu['subtitle'], curses.A_BOLD) #Subtitle for this menu


      # Display all the menu items, showing the 'pos' item highlighted

      for index in range(optioncount):

        textstyle = n

        if pos==index:

          textstyle = h

        screen.addstr(5+index,4, "%d - %s" % (index+1, menu['options'][index]['title']), textstyle)

      # Now display Exit/Return at bottom of menu

      textstyle = n

      if pos==optioncount:

        textstyle = h

      screen.addstr(5+optioncount,4, "%d - %s" % (optioncount+1, lastoption), textstyle)


      # finished updating screen


    x = screen.getch() # Gets user input


    # What is user input?

    if x >= ord('1') and x <= ord(str(optioncount+1)):

      pos = x - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index

    elif x == 258: # down arrow

      if pos < optioncount:

        pos += 1

      else: pos = 0

    elif x == 259: # up arrow

      if pos > 0:

        pos += -1

      else: pos = optioncount


  # return index of the selected item

  return pos


# This function calls showmenu and then acts on the selected item

def processmenu(menu, parent=None):

  optioncount = len(menu['options'])

  exitmenu = False

  while not exitmenu: #Loop until the user exits the menu

    getin = runmenu(menu, parent)

    if getin == optioncount:

        exitmenu = True

    elif menu['options'][getin]['type'] == COMMAND:

      curses.def_prog_mode()    # save curent curses environment


      if menu['options'][getin]['title'] == 'Pianobar':

        os.system('amixer cset numid=3 1') # Sets audio output on the pi to 3.5mm headphone jack

      screen.clear() #clears previous screen

      os.system(menu['options'][getin]['command']) # run the command

      screen.clear() #clears previous screen on key press and updates display based on pos

      curses.reset_prog_mode()   # reset to 'current' curses environment

      curses.curs_set(1)         # reset doesn't do this right


      os.system('amixer cset numid=3 2') # Sets audio output on the pi back to HDMI

    elif menu['options'][getin]['type'] == MENU:

          screen.clear() #clears previous screen on key press and updates display based on pos

          processmenu(menu['options'][getin], menu) # display the submenu

          screen.clear() #clears previous screen on key press and updates display based on pos

    elif menu['options'][getin]['type'] == EXITMENU:

          exitmenu = True


# Main program


curses.endwin() #VITAL! This closes out the menu system and returns you to the bash prompt.


Last edited by jamied_uk on 30th March 2018, 12:10; edited 1 time in total

Posts : 2348
Join date : 2010-05-09
Age : 35
Location : UK

Back to top Go down

Re: Python Menu For Linux Mint 18.3

Post by jamied_uk on 30th March 2018, 12:08

sudo apt install -y htop

sudo chmod +x


You can call it what you want and link it to an alias or function via bash rc file ( see previous vids).

Posts : 2348
Join date : 2010-05-09
Age : 35
Location : UK

Back to top Go down

Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum