1

所以我坚持解决我在 tkinter 上遇到的这个小问题。我创建了一个有两个按钮的 gui。按钮 A 附加到一个调用 python 文件的函数,该文件是一个永远运行的巨大脚本。

def startbot():
    subprocess.call("xxx.pyw",shell=True)

按钮 B 连接到一个名为 close 的函数,该函数执行root.quit()

任何熟悉 tkinter 的人都知道我接下来要说什么,当我单击按钮 A 时,tkinter 冻结并且我无法单击按钮 B。我相信这是由于 tkinter 和有关线程的一些东西,但是我不太熟悉这个话题,想知道我该如何解决这个问题?假设我可以解决这个问题,但我还有一个问题。如果我能够单击按钮 B 会关闭 tkinter 还是会停止按钮 A 的功能和 tkinter?

4

1 回答 1

2

如果我是你,我会使用subprocess.Popen

from Tkinter import Tk, Button
from subprocess import Popen

root = Tk()

def start():
    global process
    process = Popen("python /path/to/file")

def stop():
    # Uncomment this if you want the process to terminate along with the window
    # process.terminate()
    root.destroy()

Button(root, text="Start", command=start).grid()
Button(root, text="End", command=stop).grid()

root.mainloop()

当您按下Start时,脚本会在不冻结 GUI 的情况下启动。按下End将破坏窗口但保持脚本运行(除非您取消注释该行)。

于 2013-08-03T21:26:27.357 回答