can you write python code for a generic "cli menu" running under windows? it reads lines…
- User
can you write python code for a generic "cli menu" running under windows? it reads lines from a text file and then displays the multiple options. Each option has a name to diplay and a matching shell command. Both are read from the text file. You can use the arrow up and arrow down keys to navigate the menu.The cursor is simply represented as a '*' in front of the current entry. When you press enter the corresponding shell command is executed.
Assistant
Sure! Here is some sample Python code that implements a generic CLI menu under Windows, using the
pywin32module to handle the window and keyboard input:import pywin32 import os # Define the menu items and their corresponding shell commands menu_items = [ {"name": "Item 1", "command": "command1"}, {"name": "Item 2", "command": "command2"}, {"name": "Item 3", "command": "command3"}, {"name": "Item 4", "command": "command4"}, {"name": "Item 5", "command": "command5"} ] # Define the window and its layout win = pywin32.PyShell() win.SetWindowText("CLI Menu") # Create a text widget to display the menu txt = pywin32.PyText(win) txt.SetWindowText("") # Create a scrollbar and link it to the text widget scroll = pywin32.PyScrollbar(win) scroll.SetWindowText("") txt.SetScrollbar(scroll) # Set the text widget's font and background color txt.SetFont(pywin32.PyFont(10, 400, 0, 0, 0, 0, 0, 0, 0, 0, 0)) txt.SetBackground(pywin32.PyColor(255, 255, 255)) # Read the menu items from a text file with open("menu.txt", "r") as f: for line in f: line = line.strip() if line: name, command = line.split(":") menu_items.append({"name": name, "command": command}) # Display the menu for item in menu_items: txt.AppendText(item["name"] + "\n") scroll.SetScrollPos(txt.GetBestSize()) # Set the current item and highlight it current_item = menu_items[0] txt.SetSel(0, len(current_item["name"])) txt.SetBackground(py
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.