我正在尝试创建一个打开tmux的脚本,然后拆分屏幕并打开几个python实例,但它会运行第一个命令。
k = PyKeyboard()
def cntrl_b(key):
k.press_key('Control_L')
k.tap_key('b')
k.release_key('Control_L')
k.tap_key(key)
def make_win():
subprocess.call('tmux')
subprocess.call(cntrl_b('%'))
subprocess.call('clear')
subprocess.call('"')
subprocess.call('clear')
subprocess.call('ipython')
subprocess.call(cntrl_b('o'))
subprocess.call('bpython')
if __name__=='__main__':
make_win()
我称之为“清除”,因为我正在使用powerline,当我打开tmux时收到错误消息。
我已经单独测试了每个命令,它们各自都有效。
此代码只会打开tmux而不执行任何其他命令。
答案 0 :(得分:1)
您的代码运行' tmux'并等待它退出。退出tmux后,您的程序将调用您的函数cntrl_b
,该函数将返回None
。当您将其传递给subprocess.call()
时,该返回值将用作下一个程序的运行路径。这显然不是你想要的。
您想要的是运行tmux进程,然后向其发送输入。像这样:
def make_win():
proc = subprocess.Popen('tmux', stdin=subprocess.PIPE)
# TODO: look up the bytes for Ctrl+B and put that in the string
proc.stdin.write('Bo'); proc.stdin.flush()
proc.stdin.write('clear\n'); proc.stdin.flush()
proc.stdin.write('"'); proc.stdin.flush()
proc.stdin.write('clear\n'); proc.stdin.flush()
proc.stdin.write('ipython\n'); proc.stdin.flush()
# TODO: look up the bytes for Ctrl+B and put that in the string
proc.stdin.write('Bo'); proc.stdin.flush()
proc.stdin.write('bpython\n'); proc.stdin.flush()