gpt4 book ai didi

python - Curses Python 返回主菜单时不会清理窗口

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:09 27 4
gpt4 key购买 nike

6 years ago他们在这个问题上提出了一个很好的解决方案,但我的更改导致了意想不到的结果,当我调用子菜单函数和主菜单时,窗口不清晰,请帮助我了解问题所在。

import curses
from curses import panel
from test_print import get_menu_list, get_timings, time_to_seconds, GetPageChoise,\
main_menu_items, list_of_themes_end, text_discription_get

page = GetPageChoise()
class Menu(object):

def __init__(self, items, stdscreen):
self.window = stdscreen.subwin(5,2)
self.window.keypad(1)
self.panel = panel.new_panel(self.window)
self.panel.hide()
panel.update_panels()

self.position = 0
self.items = items
self.items.append(('exit','exit'))

def navigate(self, n):
self.position += n
if self.position < 0:
self.position = 0
elif self.position >= len(self.items):
self.position = len(self.items)-1

def display(self):
self.panel.top()
self.panel.show()
self.window.clear()
while True:
self.window.refresh()
curses.doupdate()
for index, item in enumerate(self.items):

if index == self.position:
mode = curses.A_REVERSE
else:
mode = curses.A_NORMAL

msg = '%d. %s' % (index, item[0])
self.window.addstr(10+ index, 1, msg, mode)
key = self.window.getch()

if key in [curses.KEY_ENTER, ord('\n')]:
if self.position == len(self.items)-1:
break
else:
self.items[self.position][1]()

elif key == curses.KEY_UP:
self.navigate(-1)

elif key == curses.KEY_DOWN:
self.navigate(1)
self.window.clear()
self.panel.hide()
panel.update_panels()
curses.doupdate()


class SubMenu(object):

def __init__(self, items, stdscreen):
self.window = stdscreen.subwin(5,2)
self.window.keypad(1)
self.panel = panel.new_panel(self.window)
self.panel.hide()
panel.update_panels()

self.position = 0
self.items = items
self.items.append(('exit','exit'))

def navigate(self, n):
self.position += n
if self.position < 0:
self.position = 0
elif self.position >= len(self.items):
self.position = len(self.items)-1

def display_sub(self):
while True:
self.window.refresh()
curses.doupdate()
for index, item in enumerate(self.items):

if index == self.position:
mode = curses.A_REVERSE
else:
mode = curses.A_NORMAL
next = 1
for disript in text_discription_get():
self.window.addstr(next, 1, disript)
next +=1
msg = '%d. %s' % (index, item[0])
self.window.addstr(10+ index, 1, msg, mode)
key = self.window.getch()
if key in [curses.KEY_ENTER, ord('\n')]:
if self.position == len(self.items)-1:
break
else:
self.items[self.position][1]()

elif key == curses.KEY_UP:
self.navigate(-1)

elif key == curses.KEY_DOWN:
self.navigate(1)


class MyApp(object):

def __init__(self, stdscreen):
self.screen = stdscreen
curses.curs_set(0)

submenu_items = [
('beep', curses.beep),
('flash', curses.flash)
]
submenu = SubMenu(sub_menu_items, self.screen) #Вывел конкретный подкаст, нужно изменить на выбор подкастов.

main_menu_items = [
('beep', curses.beep),
('flash', curses.flash),
('submenu', submenu.display_sub)
]
main_menu = Menu(main_menu_items, self.screen)
main_menu.display()

if __name__ == '__main__':
curses.wrapper(MyApp)
#Вывел меню, сделал выбор, нужно вывести Discription, сделать вызов плеера по выбору темы.

你可以在 GIF 中看到它对我的看法,事实是所提出的解决方案不适合,因为子菜单每次都应该显示新内容,具体取决于选择。 gif how work this

最佳答案

在显示子菜单的函数后面插入一个 clear() 对我有用:

class Menu(object):
...
def display(self):
...
if key in [curses.KEY_ENTER, ord('\n')]:
if self.position == len(self.items)-1:
break
else:
self.items[self.position][1]()
self.window.clear() # This will clear the main menu window every time an item is selected

您还应该在显示另一个窗口之前清除屏幕,因为如果您的子菜单小于主菜单,也可能会发生同样的情况。

或者,您可以在每个循环开始时清除窗口,因为您已经在每次按键后重新绘制列表。这样您就可以更新菜单中的项目,并且不会像使用当前程序那样看到旧菜单的部分内容。

关于python - Curses Python 返回主菜单时不会清理窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59114677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com