我再次就这个进度条项目问一个问题;虽然这只是一个澄清问题。
我的代码导致创建进度条以跟踪文件的创建。用户选择他们想要创建的文件类型,然后点击“go”,这会导致文件开始更改并显示进度条。 Progressbar很棒。文件编写/操作很有效。
问题:当用户选择要操作的多个文件时,尽管正确创建了进度条,但它们无法正确更新。起初我以为多次单击一个按钮会导致tkinter忘记之前正在执行的root.after()函数,但是在玩了一个(更简单的)示例代码后,我意识到情况并非如此。 问题:即使使用不同的参数重新启动相同的功能,如何确保tkinter不会停止实现第一个功能?
以下是我的代码部分,用于描述我在做什么。
progbarlock = False # start with the prograssbar marked as not occupied
class guibuild:
def __init__(self):
self.root = root
guibuild.progbarlock = False
global theframe
theframe = Frame(root)
job_name = e.get()
label = Label(theframe,text = job_name).pack(side=LEFT,padx =2)
self.progbar = Meter(theframe) #makes the progressbar object
self.progbar.set(0.0) #sets the initial value to 0
self.progbar.pack(side=LEFT)
self.counter = 0
self.i = float(0) #i is the value set to the progressbar
def stop_progbar(self):
self.progbar.stop()
def begin(self):
self.interval()
self.Status_bar()
theframe.pack(anchor="s")
def interval(self):
if guibuild.progbarlock == False:
guibuild.progbarlock = True
def update(self):
the_file = open('running_file.json')
data = json.load(the_file)
curr = data["current_line"]
total = data["total_lines"]
if self.i == 1.0:
self.stop_progbar
rint "100% - process is done"
self.root.after_cancel(self.interval)
elif curr == self.counter:
self.root.after(5000, self.interval)
elif curr == self.counter+1:
self.i += 1.0/total
self.progbar.set(self.i) #apply the new value of i to the progressbar
self.counter += 1
self.stop_progbar
self.root.after(5000, self.interval)
elif curr > self.counter+1:
self.i += 1.0/total*(curr-self.counter)
self.progbar.set(self.i) #apply the new value of i to the progressbar
self.counter = curr
self.stop_progbar
self.root.after(5000, self.interval)
else:
print "something is wrong - running.json is not available"
self.root.after(5000, self.interval)
guibuild.progbarlock = False
def start_process():
makeRequest() #this is defined much earlier in the code and includes all the file creation and manipulation
guibuild().begin()
button4 = Button(root,text="GO", command = start_process).pack()
注意:makeRequest()
完全取决于用户输入,每次按下“go”时用户输入都会更改。