[英]Using subprocess in a curses application
I'm creating an application that uses curses
to build a simple user interface. 我正在创建一个使用
curses
构建简单用户界面的应用程序。 It also uses the subprocess
module to run external text editor so a user can type some text, then close the editor and go back to the main program. 它还使用
subprocess
模块来运行外部文本编辑器,以便用户可以键入一些文本,然后关闭编辑器并返回主程序。
The problem is that when the editor is console-based such as Vim or Nano, curses doesn't de-initialize properly. 问题在于,当编辑器是基于控制台的,例如Vim或Nano时,curses无法正确取消初始化。 Which means if the color mode is used (
curses.start_color()
), terminal stays colored after the program is finished. 这意味着,如果使用了颜色模式(
curses.start_color()
),则在程序完成后终端仍保持彩色。
Here's a test script that has this issue (at least for me, I use Ubuntu and gnome-terminal): 这是一个有此问题的测试脚本(至少对我来说,我使用Ubuntu和gnome-terminal):
import curses
import subprocess
screen = curses.initscr()
curses.noecho()
curses.cbreak()
screen.keypad(1)
try:
curses.curs_set(0)
except curses.error:
pass
curses.start_color()
screen.addstr(0, 0, 'hello, world!')
screen.refresh()
while 1:
c = screen.getch()
if c == ord('q'):
break
if c == ord('r'):
subprocess.call('vim', shell=False)
screen.clear()
screen.addstr(0, 0, 'hello, world!')
screen.refresh()
curses.nocbreak()
screen.keypad(0)
curses.echo()
curses.curs_set(1)
curses.endwin()
(Press r
to enter Vim, then q
to exit.) (按
r
进入Vim,然后按q
退出。)
Is there a way to fix this? 有没有办法解决这个问题?
Would modifying your code to: 会将您的代码修改为:
if c == ord('q'):
subprocess.call('reset', shell=False)
break
be enough for you? 够你了吗? Or is there some additional behaviour in your real script that is not in the code you pasted here, that makes this workaround unsuitable to your goal?
还是您的真实脚本中存在一些其他行为,而不是您在此处粘贴的代码中存在其他行为,从而使此解决方法不适合您的目标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.