从GUI tkinter打开cmd和脚本

0 投票
1 回答
1004 浏览
提问于 2025-04-18 15:41

我有几个文件,一个是图形界面(GUI),还有一些脚本。我想知道怎么才能让这个图形界面打开命令提示符(cmd),这样我就可以把从图形界面获取的变量(文本)传递给我选择的脚本。

举个例子,

我从我的图形界面(gui.py)获取了一个字符串,内容是 Tonight.mp3 16 00:00:00 00:00:00 G,然后我选择了想要的脚本(segmentation.py)。

最后,我需要把这些内容传递给命令提示符,具体是:

segmentation.py Tonight.mp3 16 00:00:00 00:00:00

你有什么建议吗?我知道我需要使用 subprocess 或 os。

例如:

def printing():
    commandline = " " + a + " " + str(chunk.get()) + " " + str(start_censorship.get()) + " " + str(end_censorship.get())
    print commandline
    import subprocess
    subprocess.call(["C:\Users\Xavier_\Desktop\PROJECT\segmentation.py"])
##    cmd = subprocess.Popen(["C:\Users\Xavier_\Desktop\PROJECT\segmentation.py" , str(a), str(chunk.get()), str(start_censorship.get()), str(end_censorship.get())], shell=True, stdout=subprocess.PIPE)
##    output = cmd.communicate()   
segmentButton = Button( root,text='Segment', fg="Red",command=printing)
segmentButton.pack(side=Tkinter.TOP)

1 个回答

0

使用 communicate 方法 来获取输出。比如:

import subprocess
cmd = subprocess.Popen(["segmentation.py", str(a), str(chunk.get()), str(start_censorship.get()), str(start_censorship.get())], shell=True, stdout=subprocess.PIPE)
output = cmd.communicate()

当你把 stdout 设置为 subprocess.PIPE 时,命令的输出会通过 communicate() 返回。

撰写回答