Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.00 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import curses
  3.  
  4. def main(stdscr):
  5.   ################ Menu contents/lists
  6.   LsMenuItemsStart = []
  7.   LsMenuItemsOption2 = []
  8.   ################
  9.  
  10.   IntLoopCount = 0
  11.   IntSpaceBetweenMenuItems = 2
  12.   IntCurrentSelection = 2 # Default item in menu to be selected, 1 (first thing in list) is title
  13.   curses.curs_set(0) # Removes cursor
  14.   BoolStartMenu = True
  15.   IntLegalY, IntLegalX = stdscr.getmaxyx()
  16.  
  17.   class MenuItem:
  18.     def __init__(self, StrContent, IntOrder, LsToUse,
  19.                  BoolIsPercent=False, IntPercentage=0,
  20.                  BoolToggle=False, BoolToggleValue=False):
  21.      
  22.       self.BoolToggle = BoolToggle
  23.       self.StrContent = StrContent
  24.       self.IntOrder = IntOrder
  25.       self.LsToUse = LsToUse
  26.       self.BoolIsPercent = BoolIsPercent
  27.       self.IntPercentage = IntPercentage
  28.       self.BoolToggleValue = BoolToggleValue
  29.       self.StrSize = len(StrContent)
  30.      
  31.      
  32.       if BoolIsPercent == False:
  33.         LsToUse.insert(IntOrder, StrContent)
  34.       if BoolIsPercent == True:
  35.         IntPercentage = int(round(IntPercentage, -1)) # Should round IntPercentage to the closest 10
  36.        
  37.         self.StrSize = len(StrContent) # Stores size of string for later use if the percentage wants to be changed
  38.        
  39.         StrBar = "[" + ("#" * (int(IntPercentage / 10))) + ("-" * abs(int((IntPercentage / 10)) - 10)) + "]"
  40.      
  41.         StrContent = StrContent + " " + StrBar
  42.         LsToUse.insert(IntOrder, StrContent)
  43.        
  44.       if BoolToggle == True:
  45.         self.ChangeToggle(IntOrder, BoolToggleValue)
  46.  
  47.     def RemoveItem(self, StrContent):
  48.       self.LsToUse.remove(self.StrContent)
  49.  
  50.     def ChangePercentage(self, IntIndex, IntPercentageChange):
  51.       self.IntIndexInList = IntIndex
  52.       self.IntPercentageToChangeTo = IntPercentageChange
  53.       self.IntPercentage = IntPercentageChange
  54.      
  55.       StrInIndex = self.LsToUse.pop(IntIndex)
  56.       IntPercentage = int(round(IntPercentageChange, -1))  # Should round IntPercentage to the closest 10
  57.       if self.BoolIsPercent == False: # Checks just incase the string doesn't allready have a bar
  58.         StrBar = "[" + ("#" * (int(IntPercentageChange / 10))) + ("-" * abs(int((IntPercentageChange / 10)) - 10)) + "]"
  59.         StrNewString = StrInIndex + " " + StrBar
  60.         self.LsToUse.insert(IntIndex, StrNewString)
  61.       else:
  62.         StrBar = "[" + ("#" * (int(IntPercentageChange / 10))) + ("-" * abs(int((IntPercentageChange / 10)) - 10)) + "]"
  63.         StrNewString = StrInIndex[:self.StrSize] + " " + StrBar
  64.         self.LsToUse.insert(IntIndex, StrNewString)
  65.    
  66.     def ChangeToggle(self, IntIndex, BoolToggleValue):
  67.      
  68.       StrInIndex = self.LsToUse.pop(IntIndex)
  69.      
  70.       if BoolToggleValue == True:
  71.         StrNewString = StrInIndex[:self.StrSize] + " [#]"
  72.         self.BoolToggleValue = True
  73.       elif BoolToggleValue == False:
  74.         StrNewString = StrInIndex[:self.StrSize] + " []"
  75.         self.BoolToggleValue = False
  76.       self.LsToUse.insert(IntIndex, StrNewString)
  77.  
  78.  
  79.  
  80.   stdscr.clear()
  81.  
  82.  
  83.   ##### Start Menu
  84.   Title = MenuItem("Script", 0, LsMenuItemsStart)
  85.   Option1 = MenuItem("Option 1", 1, LsMenuItemsStart, True, 50)
  86.   Option2 = MenuItem("Option 2", 2, LsMenuItemsStart)
  87.   OptionToggle = MenuItem("Option Toggle", 3, LsMenuItemsStart, False, 0, True, True)
  88.   OptionExit = MenuItem("Exit", 4, LsMenuItemsStart)
  89.   # Otions 6-32 are just so I can test my program when more items then can fit on screen are presented
  90.   Option6 = MenuItem("Option6", 5, LsMenuItemsStart)
  91.   Option7 = MenuItem("Option7", 6, LsMenuItemsStart)
  92.   Option8 = MenuItem("Option8", 7, LsMenuItemsStart)
  93.   Option9 = MenuItem("Option9", 8, LsMenuItemsStart)
  94.   Option10 = MenuItem("Option10", 9, LsMenuItemsStart)
  95.   Option11 = MenuItem("Option11", 10, LsMenuItemsStart)
  96.   Option12 = MenuItem("Option12", 11, LsMenuItemsStart)
  97.   Option13 = MenuItem("Option13", 12, LsMenuItemsStart)
  98.   Option14 = MenuItem("Option14", 13, LsMenuItemsStart)
  99.   Option15 = MenuItem("Option15", 14, LsMenuItemsStart)
  100.   Option16 = MenuItem("Option16", 15, LsMenuItemsStart)
  101.   Option17 = MenuItem("Option17", 16, LsMenuItemsStart)
  102.   Option18 = MenuItem("Option18", 17, LsMenuItemsStart)
  103.   Option19 = MenuItem("Option19", 18, LsMenuItemsStart)
  104.   Option20 = MenuItem("Option20", 19, LsMenuItemsStart)
  105.   Option21 = MenuItem("Option21", 20, LsMenuItemsStart, True, 50)
  106.   Option22 = MenuItem("Option22", 21, LsMenuItemsStart, False, 0, True, True)
  107.   Option23 = MenuItem("Option23", 22, LsMenuItemsStart)
  108.   Option24 = MenuItem("Option24", 23, LsMenuItemsStart)
  109.   Option25 = MenuItem("Option25", 24, LsMenuItemsStart)
  110.   Option26 = MenuItem("Option26", 25, LsMenuItemsStart)
  111.   Option27 = MenuItem("Option27", 26, LsMenuItemsStart)
  112.   Option28 = MenuItem("Option28", 27, LsMenuItemsStart)
  113.   Option29 = MenuItem("Option29", 28, LsMenuItemsStart)
  114.   Option30 = MenuItem("Option30", 29, LsMenuItemsStart)
  115.   Option31 = MenuItem("Option31", 30, LsMenuItemsStart)
  116.   Option32 = MenuItem("Option32", 31, LsMenuItemsStart)
  117.  
  118.   ##### Option 2 menu
  119.   TitleOption2 = MenuItem("Script", 0, LsMenuItemsOption2)
  120.   Option4 = MenuItem("Option 4", 1, LsMenuItemsOption2)
  121.   Option5 = MenuItem("Opyion 5", 2, LsMenuItemsOption2)
  122.   Back = MenuItem("Back", 3, LsMenuItemsOption2)
  123.  
  124.   def CreateMenu(LsMenuItems):
  125.     stdscr.clear()
  126.     IntLoopCount = 0
  127.     IntNextItemY = 0
  128.     IntCurrentPage = 1
  129.     IntThisPageLoopCount = 0
  130.    
  131.     for Item in LsMenuItems:  # Sets up start menu
  132.       IntLoopCount = IntLoopCount + 1
  133.       if IntNextItemY < IntLegalY - 1:
  134.         if IntLoopCount == 1 and IntCurrentPage == 1:  # Title
  135.           stdscr.addstr(0, 0, Item, curses.A_BOLD)
  136.           IntNextItemY = IntLoopCount + IntSpaceBetweenMenuItems + 1
  137.           continue  # Skip rest of current iteration
  138.         if IntLoopCount == IntCurrentSelection:
  139.           curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
  140.           stdscr.addstr(IntNextItemY, 0, Item, curses.color_pair(1))
  141.         else:
  142.           stdscr.addstr(IntNextItemY, 0, Item)
  143.         IntNextItemY = IntNextItemY + IntSpaceBetweenMenuItems
  144.       else:
  145.         stdscr.addstr(IntLegalY - 1, 0, "~")
  146.         if IntLoopCount == IntCurrentSelection: # Goes to next page
  147.           stdscr.clear()
  148.           IntCurrentPage = IntCurrentPage + 1
  149.           IntNextItemY = 0
  150.           stdscr.addstr(IntNextItemY, 0, Item, curses.color_pair(1))
  151.           IntNextItemY = IntNextItemY + IntSpaceBetweenMenuItems
  152.          
  153.  
  154.   while True:
  155.    
  156.     ############################ Start Menu
  157.     while BoolStartMenu == True:
  158.       CreateMenu(LsMenuItemsStart)
  159.      
  160.       stdscr.refresh()
  161.    
  162.       IntKey = stdscr.getch() # Wait for user input
  163.    
  164.       if IntKey == ord("q"): # ord("letter") returns ASCII of letter
  165.         exit()
  166.        
  167.       if IntKey == curses.KEY_UP and IntCurrentSelection > 2:
  168.         IntCurrentSelection = IntCurrentSelection - 1
  169.        
  170.       elif IntKey == curses.KEY_DOWN and IntCurrentSelection < len(LsMenuItemsStart):
  171.         IntCurrentSelection = IntCurrentSelection + 1
  172.      
  173.       elif IntKey == curses.KEY_LEFT:
  174.         if IntCurrentSelection == 2 and Option1.IntPercentage is not 0 and Option1.BoolIsPercent == True:
  175.           Option1.ChangePercentage(1, Option1.IntPercentage - 10)
  176.         if IntCurrentSelection == 21 and Option21.IntPercentage is not 0 and Option21.BoolIsPercent == True:
  177.           Option21.ChangePercentage(20, Option21.IntPercentage - 10)
  178.        
  179.       elif IntKey == curses.KEY_RIGHT:
  180.         if IntCurrentSelection == 2 and Option1.IntPercentage is not 100 and Option1.BoolIsPercent == True:
  181.           Option1.ChangePercentage(1, Option1.IntPercentage + 10)
  182.        
  183.         elif IntCurrentSelection == 21 and Option21.IntPercentage is not 100 and Option21.BoolIsPercent == True:
  184.           Option21.ChangePercentage(20, Option21.IntPercentage + 10)
  185.      
  186.       elif IntKey == 32: # ASCII code 32 is space bar
  187.         if IntCurrentSelection == 4 and OptionToggle.BoolToggleValue == True:
  188.           OptionToggle.ChangeToggle(3, False)
  189.         elif IntCurrentSelection == 4 and OptionToggle.BoolToggleValue == False:
  190.           OptionToggle.ChangeToggle(3, True)
  191.         if IntCurrentSelection == 22 and Option22.BoolToggleValue == True:
  192.           Option22.ChangeToggle(21, False)
  193.         elif IntCurrentSelection == 22 and Option22.BoolToggleValue == False:
  194.           Option22.ChangeToggle(21, True)
  195.      
  196.       elif IntKey == 10: # ASCII code 10 is the enter key, curses.KEY_ENTER doesn't work for some reason
  197.        
  198.         ##### Most code for actually "doing stuff" depending on what item is pressed should go here
  199.         if IntCurrentSelection == 5: # Adjust number depending on where the "exit" or "back" button is
  200.           exit()
  201.         if IntCurrentSelection == 3:
  202.           BoolStartMenu = False
  203.           BoolOption2Menu = True
  204.        
  205.    
  206.     ############################ Option 2 menu
  207.     while BoolOption2Menu == True:
  208.       stdscr.clear()
  209.       CreateMenu(LsMenuItemsOption2)
  210.       stdscr.refresh()
  211.  
  212.       IntKey = stdscr.getch()  # Wait for user input
  213.  
  214.       if IntKey == ord("q"):  # ord("letter") returns ASCII of letter
  215.         exit()
  216.       if IntKey == curses.KEY_UP and IntCurrentSelection > 2:
  217.         IntCurrentSelection = IntCurrentSelection - 1
  218.       elif IntKey == curses.KEY_DOWN and IntCurrentSelection < len(LsMenuItemsOption2):
  219.         IntCurrentSelection = IntCurrentSelection + 1
  220.       elif IntKey == 10:  # ASCII code 10 is the enter key, curses.KEY_ENTER doesn't work for some reason
  221.        
  222.         ##### Most code for actually "doing stuff" depending on what item is pressed should go here
  223.         if IntCurrentSelection == 4:  # Adjust number depending on where the "exit" or "back" button is
  224.           BoolStartMenu = True
  225.           BoolOption2Menu = False
  226.         if IntCurrentSelection == 3:
  227.           pass
  228.          
  229.  
  230. # Does initialisation described at https://docs.python.org/3/howto/curses.html and also handles reseting
  231. # the terminal if an error occurs
  232. curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement