类中的变量在使用包键盘后不会更新



我正在使用包"键盘";以创建控制台菜单。

我想在检查确认时进行检查。然而,问题是,如果你点击确认,(self.current将等于选项的长度(当self.up或self.down更改时,定义运行中的self.currents不会更改。

注意:不包括self.up或self.down,因为在self.run中,self.current更改

class menu:
def __init__(self, maxChoices=1):
self.maxChoices = maxChoices
self.current = 0
self.choices = ["Do something 1", "Do something 2", "Do something 3", "Do something 4", "Do something 5"]
self.choiceSelected = []
self.error = ""
def run(self):
while True:
self.show_menu(self.error)
keyboard.add_hotkey('up', self.up)
keyboard.add_hotkey('down', self.down)
keyboard.wait("enter")
keyboard.unhook_all_hotkeys()
if self.current == len(self.choices) and len(self.choiceSelected) != 0:
return self.choiceSelected
elif self.current == len(self.choices):
self.error = "n" + bcolors.WARNING + "ERROR: Nothing selected!" + bcolors.ENDC
str(self.current)
input()
def show_menu(self, error=""):
clearConsole()
print(self.current)
print("Choose an option:")
print('┌' + '─' * max(len(s) + 4 for s in self.choices) + '┐')
for choiceNum, choice in enumerate(self.choices):
spaces = " " * (max(len(s) for s in self.choices) - len(self.choices[choiceNum]))
if choiceNum == self.current:
if choiceNum in self.choiceSelected:
print(f'│{bcolors.BOLD + bcolors.OKGREEN}>>{self.choices[choiceNum]}<<{bcolors.ENDC}{spaces}│')
else:
print(f'│{bcolors.BOLD}>>{self.choices[choiceNum]}<<{bcolors.ENDC}{spaces}│')
elif choiceNum in self.choiceSelected:
print(f'│{bcolors.OKGREEN}  {self.choices[choiceNum]}{bcolors.ENDC}{spaces}  │')
else:
print(f'│  {self.choices[choiceNum]}{spaces}  │')
spaces = " " * (max(len(s) for s in self.choices) - 7)
if self.current == len(self.choices):
print(f'│>>{bcolors.FAIL}CONFIRM{bcolors.ENDC}<<{spaces}│')
else:
print(f'│  {bcolors.OKBLUE}CONFIRM{bcolors.ENDC}{spaces}  │')
print('└' + '─' * max(len(s) + 4 for s in self.choices) + '┘')
print(error)

找到了答案。。。。

我忘了清除self.error,所以每当按下回车键后,self.error就会打印出来。

让我失望的是,当我尝试输入时,在终端上它会消失,所以我无法检查当前的真实值

最新更新