【发布时间】:2021-11-18 15:52:08
【问题描述】:
我编写的程序是一个菜单,可通过键盘模块的实时键盘输入进行导航;与在 python 中创建的通过设置用户输入(input())导航的标准菜单不同,此菜单在使用时应该具有类似滚动的效果。 代码:
def MenuInterface():
import keyboard
MenuList = ["""Welcome to Empires Shell
> [PLAY]
[HELP]
[CREDITS]
[EXIT]
""", """Welcome to Empires Shell
[PLAY]
> [HELP]
[CREDITS]
[EXIT]""", """Welcome to Empires Shell
[PLAY]
[HELP]
> [CREDITS]
[EXIT]
""", """Welcome to Empires Shell
[PLAY]
[HELP]
[CREDITS]
> [EXIT]
"""]
print (MenuList[0])
x = 0
while True: #This is the actual loop where I'm encountering my error
if keyboard.read_key() == "s":
x = x + 1
if x == -1:
x = 3
print (MenuList[x])
elif x == 4:
x = 0
print (MenuList[x])
else:
print (MenuList[x])
MenuInterface()
跑步回报:
Welcome to Empires Shell
> [PLAY]
[HELP]
[CREDITS]
[EXIT]
在shell中输入“s”后,返回:
Welcome to Empires Shell
[PLAY]
> [HELP]
[CREDITS]
[EXIT]
Welcome to Empires Shell
[PLAY]
[HELP]
> [CREDITS]
[EXIT]
如您所见,keyboard.read 为单个输入运行了两次。你知道为什么吗?如果是这样,我该如何解决这个问题? 谢谢!
【问题讨论】:
标签: python-3.x function input module