本帖最后由 kk159 于 2022-1-6 16:14 编辑
大致实现效果如下:
PS.感觉功能虽然实现,貌似不是完美的解决办法,
有更好的处理,望指导留言.....
完整代码:
[Python] 纯文本查看 复制代码 import keyboard
import os
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Menu():
key = 0
order = ['11111', '2222', '3333']
def __init__(self):
self.menu()
keyboard.add_hotkey('up', lambda: self.upPress())
keyboard.add_hotkey('down', lambda: self.downPress())
keyboard.add_hotkey('enter', lambda: self.enterPress())
keyboard.wait('esc')
def menu(self):
os.system('cls')
for i in range(len(self.order)):
prev = Colors.OKBLUE + ' '
if self.key == i:
prev = ">>" + Colors.HEADER
print(prev + self.order[i])
def upPress(self):
if self.key == 0:
self.key = len(self.order) - 1
else:
self.key -= 1
self.menu()
def downPress(self):
if self.key == len(self.order) - 1:
self.key = 0
else:
self.key += 1
self.menu()
def enterPress(self):
print(Colors.OKGREEN + self.order[self.key])
if __name__ == '__main__':
m = Menu() |