我在我的curses程序中按'i'時試圖放入IPython shell。爲了達到這一點,我設置了一個curses窗口和其他幾個窗口。我可以使用while循環與程序進行交互,並根據按鍵進行操作。如何將IPython shell嵌入到使用curses模塊的Python程序中?
從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年開始的。下面是[當前的嵌入文檔](http://ipython.org/ipython-doc/stable/interactive/reference.html#埋設 - IPython中)。 – minrk 2012-01-08 19:51:35
謝謝指出。但是,我使用IPython 0.10.2和[文檔](http://ipython.org/ipython-doc/rel-0.10.2/html/interactive/reference.html)沒有什麼不同,至少嵌入部分。 – 2012-01-09 23:50:56