用于具有Python Tk线程和os.system调用的shell命令的基本GUI
我正在做一个基本的GUI,在一些shell命令之后提供一些用户反馈,一个shell脚本的小界面 显示TK窗口,等待os.system调用完成,并在每次os.system调用后多次更新TK窗口 线程是如何使用tk的用于具有Python Tk线程和os.system调用的shell命令的基本GUI,python,shell,command-line,tk,Python,Shell,Command Line,Tk,我正在做一个基本的GUI,在一些shell命令之后提供一些用户反馈,一个shell脚本的小界面 显示TK窗口,等待os.system调用完成,并在每次os.system调用后多次更新TK窗口 线程是如何使用tk的 就这样,谢谢 如果您使用Tk运行标准线程库,它应该完全正常。源代码说,您应该让主线程运行gui,并为您的os.system()调用创建线程 您可以编写这样的抽象,在完成任务后更新GUI: def worker_thread(gui, task): if os.system(st
就这样,谢谢 如果您使用Tk运行标准线程库,它应该完全正常。源代码说,您应该让主线程运行gui,并为您的
os.system()
调用创建线程
您可以编写这样的抽象,在完成任务后更新GUI:
def worker_thread(gui, task):
if os.system(str(task)) != 0:
raise Exception("something went wrong")
gui.update("some info")
可以使用标准库中的
thread.start_new_thread(函数,args[,kwargs])
启动线程。请参阅文档。这只是我所做工作的一个基本示例,康斯坦丁尼乌斯指出线程可以与Tk一起工作,这要归功于康斯坦丁尼乌斯
import sys, thread
from Tkinter import *
from os import system as run
from time import sleep
r = Tk()
r.title('Remote Support')
t = StringVar()
t.set('Completing Remote Support Initalisation ')
l = Label(r, textvariable=t).pack()
def quit():
#do cleanup if any
r.destroy()
but = Button(r, text='Stop Remote Support', command=quit)
but.pack(side=LEFT)
def d():
sleep(2)
t.set('Completing Remote Support Initalisation, downloading, please wait ')
run('sleep 5') #test shell command
t.set('Preparing to run download, please wait ')
run('sleep 5')
t.set("OK thanks! Remote Support will now close ")
sleep(2)
quit()
sleep(2)
thread.start_new_thread(d,())
r.mainloop()
谢谢康斯坦丁尼乌斯,我会查一查的!康斯坦丁尼乌斯多亏了你的投入,我这里有一个完整的例子:如果你把它包括在你的答案中,那就太好了。