I want to create some kind of list or sequence of characters/words that can be navigated using the arrow keys and selected using enter, is there any way to simply do this kind of thing in Python?
-
sure use pycurses ... but it might not work on your operating system ... without alot of work at least – Joran Beasley Mar 10 '15 at 22:18
-
I guess you want to develop a command line user interface in python. If yes, this should help you: http://stackoverflow.com/questions/10873157/python-interactive-cli-application – user3885927 Mar 10 '15 at 22:18
-
hey, would some of you who downvoted at least comment why so the OP understands what you feel is wrong with this question? – cure Mar 10 '15 at 23:12
-
@nephi12: This question shows absolutely no evidence of reasearch by the author, and the question is unclear. Are they developing a GUI? Console app? – Bryan Oakley Mar 11 '15 at 13:12
2 Answers
#!/usr/bin/python
import sys, tty, termios, os
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
current = 0
lst = []
for i in range(0,10):
lst.append(i)
while 1:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
os.system("clear")
for i in range(0,len(lst)):
if i == current:
print str(lst[i]) + "*"
else:
print str(lst[i])
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
if ord(ch) not in [27, 13]:
continue
elif ord(ch) == 27:
k = []
k.append(ord(sys.stdin.read(1)))
k.append(ord(sys.stdin.read(1)))
if k[0] == 91:
if k[1] == 65:
if current > 0:
current -= 1
elif k[1] == 66:
if current < len(lst)-1:
current += 1
elif ord(ch) == 13:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
break
print "you chose: " + str(current)
this is some code i wrote up to demonstrate it, i wrote it in ten minutes so its not exactly perfect, but it runs under python 2.7 on Linux. the basic idea is you change the terminal mode to raw input output, read for arrow keys and enter, and change a variable current
that points to the currently selected menu item. as i mentioned, i wrote this up in 10 minutes, so with a bit of research you should be able to come up with an interface of your own. if you have any questions specific to this answer, please comment them.
if you want to use on windows, try msvcrt.getch(), you can write a function yourself to clear the screen or use os.system("cls") on windows, etc...

- 2,588
- 1
- 17
- 25
here some basic example in tkinter + python 3.4:
fast video : video through a list using the keyboard
import tkinter
class aplicacion():
def __init__(self,principal):
self.counter = -1
self.list = ["a", "b", "c", "d", "e"] # here the list
self.right = principal
self.right.bind("<Right>", self.keyright)
self.left = principal
self.left.bind("<Left>", self.keyleft)
self.enter = principal
self.enter.bind("<Return>", self.keyenter)
self.label_texto = tkinter.Label(principal, text= "here the text appear in the list")
self.label_texto.grid(column=0, row=0)
def keyright(self, event):
if self.counter == len(self.list)-1:
print("ya recorrio toda la lista, se reiniciara")
self.label_texto.config(text = "ya recorrio toda la lista, se reiniciara")
self.counter = 0
self.show_list_element(self.counter)
else:
self.counter +=1
self.show_list_element(self.counter)
def keyleft(self, event):
if self.counter < -1:
self.counter = len(self.list) -1
self.show_list_element(self.counter)
else:
self.counter -=1
self.show_list_element(self.counter)
def keyenter(self, event):
self.choise = self.label_texto.cget("text")
self.label_texto.config(text = ("you select: " + self.choise))
def show_list_element(self, account):
self.account = account
print(self.list[self.counter])
self.label_texto.config(text = self.list[self.account])
#widgets
root = tkinter.Tk()
#resolucionX = root.winfo_screenwidth()
#resolucionY = root.winfo_screenheight()
#root.geometry(str(resolucionX)+"x"+str(resolucionY)+"+0+0")
root.title("List Exercise")
app = aplicacion(principal=root)
root.mainloop()

- 177
- 9