我在谷歌上搜索了很多,但仍然没有找到我要找的东西。 我想这是一个经典问题,但我仍然无法弄清楚。
我有这个 Python/Tkinter 代码。该代码通过使用 os.system(cmd) 简单地调用它来启动一个占用大量 CPU 资源的进程。我想要一个进度条(振荡的,而不是渐进的),它向用户显示实际正在发生的事情。
我想我只需要在调用 os.system 之前启动包含进度条的线程,然后在进度条线程运行时调用 os.system ,关闭进度条线程并销毁关联Toplevel()
。
我的意思是,Python 非常灵活,是否可以轻松地做到这一点? 我知道从另一个线程杀死一个线程是不安全的(由于数据共享),但是据我所知,这两个线程不共享任何数据。
是否可以这样:
progressbar_thread.start()
os.system(...)
progressbar_thread.kill()
如果这是不可能的,我仍然不明白如何在两个线程之间传递“信号”变量。
谢谢,
安德里亚
最佳答案
这就是你所追求的东西吗?
from Tkinter import *
import ttk, threading
class progress():
def __init__(self, parent):
toplevel = Toplevel(tk)
self.progressbar = ttk.Progressbar(toplevel, orient = HORIZONTAL, mode = 'indeterminate')
self.progressbar.pack()
self.t = threading.Thread()
self.t.__init__(target = self.progressbar.start, args = ())
self.t.start()
#if self.t.isAlive() == True:
# print 'worked'
def end(self):
if self.t.isAlive() == False:
self.progressbar.stop()
self.t.join()
def printmsg():
print 'proof a new thread is running'
tk = Tk()
new = progress(tk)
but1 = ttk.Button(tk, text= 'stop', command= new.end)
but2 = ttk.Button(tk, text = 'test', command= printmsg)
but1.pack()
but2.pack()
tk.mainloop()
关于Python,Tkinter - ttk.Progressbar 在单独的线程中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353090/