python - 如何将IPython shell嵌入到使用curses模块的Python程序中?

标签 python terminal ipython ncurses curses

当我在诅咒程序中按“i”时,我试图进入 IPython shell。到目前为止,我设置了一个curses 窗口和几个其他窗口。我可以使用 while 循环与程序交互并根据击键进行操作。

来自Embedding IPython我借用了一些代码,您可以在 block 的开头和结尾看到这些代码。

...

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()

...

def main():

stdscr = curses.initscr()

(win_maxy, win_maxx) = stdscr.getmaxyx()

onethirds_win_width = int(math.floor(win_maxx/3))
half_win_heigth = int(math.floor(win_maxy/2))

begin_x = 0; begin_y = 0
height = half_win_heigth ; width = onethirds_win_width
download_win = curses.newwin(height, width, begin_y, begin_x)
download_win.border()
download_win.refresh()
download_list_win = download_win.subwin(height-2, width-2, begin_y+1, begin_x+1)

begin_x = (half_win_heigth-1)+2 ; begin_y = 0
height = half_win_heigth ; width = onethirds_win_width
configs_win = curses.newwin(height, width, begin_y, begin_x)
configs_win.border()
configs_win.refresh()
configs_list_win = configs_win.subwin(height-2, width-2, begin_y+1, begin_x+1)

begin_x = 0 ; begin_y = win_maxy-1
height = 1 ; width = win_maxx
status_win = curses.newwin(height, width, begin_y, begin_x)

begin_x = 0 ; begin_y = half_win_heigth+2
height = 1 ; width = win_maxx
input_win = curses.newwin(height, width, begin_y, begin_x)
input_win_textbox = curses.textpad.Textbox(input_win)
#curses.textpad.rectangle(stdscr, begin_y, begin_x, begin_y+height+1, begin_x+width+1)

curses.noecho()
curses.cbreak()
stdscr.keypad(1)

stdscr.addstr(half_win_heigth+5, 5, "MCP Fetcher. Ready.")
stdscr.refresh()

while 1:
    c = stdscr.getch()
    download_win.border()
    if c == ord('q'): break  # Exit the while()
    elif c == ord('c'): stdscr.clear()  # Clear the screen
    elif c == ord('i'):
        curses.nocbreak(); stdscr.keypad(0); curses.echo()
        ipshell()  # Start the ipython shell
        curses.noecho(); curses.cbreak(); stdscr.keypad(1)

...

代码继续。

问题是IPython shell没有echo,没有cbreak等。我该如何解决这个问题?

谢谢

最佳答案

这里有一个可能有帮助的技巧:Working with curses in IPython. How can I improve this?

...它有一些问题,但基本功能已经有了。

关于python - 如何将IPython shell嵌入到使用curses模块的Python程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8777713/

相关文章:

python - 如何在 Python 中将日期时间转换为 UTC 时间戳?

python 3 : Using a multiprocessing queue for logging

Python 3 - Pipenv 无法安装出现错误

python - 在 Ubuntu 终端中,我怎样才能让 python 程序继续运行?

python - 当在 while 语句内执行 input() 时,Jupyter 随机繁忙(卡住)

IPython 选项卡完成菜单颜色不可读

python - 您可以获得 Google Cloud Composer/Airflow 的静态外部 IP 地址吗?

python - 使用 Jinja 模板根据单元格值对表格中的单元格进行颜色编码

terminal - autoconf在homebrew中没有链接,如何链接它?

python - 使用 IPython Notebook Web 界面移动 .ipynb