【问题标题】:How can I embed IPython shell into a Python program which uses curses module?如何将 IPython shell 嵌入到使用 curses 模块的 Python 程序中?
【发布时间】:2012-01-08 12:58:55
【问题描述】:

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

Embedding IPython我借了一些代码,你可以在块的开头和结尾看到。

...

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等,我该如何解决?

谢谢

【问题讨论】:

  • 也许与这个问题特别无关,但您链接的文档来自 2005 年。这里是 the current embedding docs
  • 感谢您的指出。但是,我使用 IPython 0.10.2 和 documentation 并没有什么不同,至少是嵌入部分。

标签: python terminal ipython ncurses curses


【解决方案1】:

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

...它有一些问题,但基本功能在那里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    相关资源
    最近更新 更多