问题描述
我正在使用 python 和 tkinter 为其他同事编写某种 gui 工具。 我已经几乎完成了这个工具,最后我想为他们制作一个进度条,以便他们确认进度。 其概念是编写一个运行在另一个线程上的事件驱动系统(调用者事件管理器),一旦事件管理器检测到某种目标事件已经发送,它就会立即调用目标函数插入主线程的for循环。 这是我的示例代码:
from queue import Queue,Empty
from threading import *
from tkinter import *
import time
from tkinter import ttk
EVENT_TYPE_1 = "Count"
MAX_NUMBER = 10
CUR_NUMBER = 0
class event_manager:
def __init__(self):
self._eventQueue = Queue()
self._thread = Thread(target=self.Run)
self._handlers = {}
self._active = False
def Start(self):
self._active = True
self._thread.start()
def Run(self):
while self._active is True:
try:
event = self._eventQueue.get(block=True,timeout=1)
self.Process(event)
except Empty:
pass
def Process(self,event):
if event.type in self._handlers:
for handler in self._handlers[event.type]:
handler()
else:
pass
def Stop(self):
self._active = False
self._thread.join()
def addEventListenter(self,type_,handler):
try:
handlerList = self._handlers[type_]
except KeyError:
handlerList = []
self._handlers[type_] = handlerList
if handler not in handlerList:
handlerList.append(handler)
def removeEventListenter(self,handler):
try:
handlerList = self._handlers[type_]
if handler in handlerList:
handlerList.remove(handler)
if not handlerList:
del self._handlers[type_]
except KeyError:
pass
def sendEvent(self,event):
self._eventQueue.put(event)
class Event:
def __init__(self,event_event_name,cur_done_task,type_=None):
self.type = type_
self._event_name = event_event_name
self._curDoneTask = cur_done_task
class EventSource:
def __init__(self,event_name,event_mgr,max_number,type):
self._event_name = event_name
self._event_manager = event_mgr
self._type = type
self._max_number = max_number
def count(self):
global CUR_NUMBER
for i in range(self._max_number):
CUR_NUMBER = i + 1
print("************ main thread start:Now start process {} - count : {}".format(self._event_name,CUR_NUMBER))
event = Event("test",CUR_NUMBER,type_=self._type)
self._event_manager.sendEvent(event)
time.sleep(1)
class GUIListener(Tk):
def __init__(self):
super(GUIListener,self).__init__()
self.title("Progress GUI")
self.geometry("1200x805+600+100")
self.config(bg="#535353")
self.resizable(True,True)
self.progressBar = ttk.Progressbar(master=self,orient=HORIZONTAL,maximum=MAX_NUMBER,length=300)
self.progressBar.pack()
self.button = ttk.Button(self,text="Run",command=lambda: self.button_function(MAX_NUMBER))
self.button.pack()
def update_progress_value(self):
print("************Sub thread start: detect progress bar value is Now...{}".format(self.progressBar['value']))
self.progressBar['value'] = CUR_NUMBER
self.progressBar.update_idletasks()
print("************Sub thread start: update progress bar value to...{}".format(CUR_NUMBER))
def button_function(self,max_number):
es = EventSource("eventSource",eventMgr,EVENT_TYPE_1)
es.count()
if __name__ == '__main__':
gui = GUIListener()
eventMgr = event_manager()
eventMgr.addEventListenter(EVENT_TYPE_1,gui.update_progress_value)
eventMgr.Start()
gui.mainloop()
但是结果出乎我的意料,控制台直接给我
************ main thread start:Now start process eventSource - count : 1
************ main thread start:Now start process eventSource - count : 2
************ main thread start:Now start process eventSource - count : 3
************ main thread start:Now start process eventSource - count : 4
************ main thread start:Now start process eventSource - count : 5
************ main thread start:Now start process eventSource - count : 6
************ main thread start:Now start process eventSource - count : 7
************ main thread start:Now start process eventSource - count : 8
************ main thread start:Now start process eventSource - count : 9
************ main thread start:Now start process eventSource - count : 10
************Sub thread start: detect progress bar value is Now...0.0
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
************Sub thread start: detect progress bar value is Now...10
************Sub thread start: update progress bar value to...10
那么我的问题在哪里,我怎样才能得到这样的结果
************ main thread start:Now start process eventSource - count : 1
************Sub thread start: detect progress bar value is Now...0.0
************Sub thread start: update progress bar value to...1
************ main thread start:Now start process eventSource - count : 2
************Sub thread start: detect progress bar value is Now...1.0
************Sub thread start: update progress bar value to...2
************ main thread start:Now start process eventSource - count : 3
************Sub thread start: detect progress bar value is Now...2.0
************Sub thread start: update progress bar value to...3
...
etc
这个问题困扰了我大约一个星期,请帮助我。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)