我正在尝试为 Raspberry Pi 的超声波传感器制作一个基本的 TUI。我在从 getstr() 获取用户输入时遇到问题。该程序允许您键入和输入值,但它返回一个空字符串。这是接口的代码:
def interface(inputs):
screen = curses.initscr()
curses.noecho()
screen.border(0)
screen.keypad(1)
screen.nodelay(0)
dim = screen.getmaxyx()
selection = -1
if inputs == 0:
option = 0
elif inputs == 1:
option = 1
elif inputs == 2:
option = 2
elif inputs == 3:
option = 3
while selection < 0:
graphics = [0]*5
graphics[option] = curses.A_REVERSE
screen.addstr(2, dim[1]/2-15, 'ULTRASONIC DISTANCE SENSOR 1.0', curses.A_BOLD)
screen.addstr(10, 10, 'Number of pings: ', graphics[0])
screen.addstr(11, 10, 'Number of seconds between pings: ', graphics[1])
screen.addstr(12, 10, 'Number of seconds before starting: ', graphics[2])
screen.addstr(13, 10, 'Start', graphics[3])
screen.addstr(14, 10, 'Exit', graphics[4])
action = screen.getch()
if action == curses.KEY_UP:
option = (option - 1) % 5
elif action == curses.KEY_DOWN:
option = (option + 1) % 5
elif action == ord('\n'):
selection = option
if selection == 0:
curses.echo()
pings = screen.getstr(10, 27, 3)
screen.addstr(10, 30, pings)
screen.refresh
pings = int(pings)
curses.noecho()
curses.endwin()
interface(0)
elif selection == 1:
curses.echo()
seconds = screen.getstr(11, 43, 3)
seconds = int(seconds)
curses.noecho
interface(1)
elif selection == 2:
curses.echo()
bstart = screen.getstr(12, 45, 3)
bstart = int(bstart)
curses.noecho
interface(2)
elif selection == 3:
window = curses.newwin(20, 40, 10, dim[1]/2+5)
window.border(0)
wdim = window.getmaxyx()
window.addstr(1, wdim[1]/2-6, "Measurements")
window.refresh()
time.sleep(bstart)
for y in range(pings):
window.clear()
window.addstr(y, 0, 'ping')
window.refresh()
time.sleep(0.1)
screen.getch()
curses.endwin()
elif selection == 4:
curses.endwin()
curses.endwin()