我有一个桌面图标(Linux Mint 系统),它调用脚本(conky),但也需要退出,所以我没有一直打开终端窗口。我正在尝试将其转换为屏幕上的 python 按钮。
我可以让 Python 按钮运行脚本,但一旦脚本启动并正常运行,我就无法让它关闭终端。
这是正常命令:/home/user/.conky_start.sh && exit
当前 Python 按钮脚本:
import Tkinter
from Tkinter import *
import subprocess
top = Tkinter.Tk()
def run_conky():
subprocess.call(['/home/user/.conky_start.sh', '&&', 'exit'], shell=True)
B = Tkinter.Button(top, text ="Conky", command = run_conky)
B.pack()
top.mainloop()
最佳答案
您应该使用:
subprocess.call('/home/user/.conky_start.sh && exit', shell=True)
当shell=True
时,call
的第一个参数必须是代表命令行的字符串。如果传递列表,第一个元素将用作命令行,其他参数将作为选项传递到底层 shell。
换句话说,代码中的 && exit
从未执行,因为它作为选项传递给 shell(可能只是忽略了无法识别的选项)。
参见this有关 subprocess
函数参数的更多信息的问题。
关于python - 如何让 Python 2.7 处理包含 "&& exit"的 shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28010184/