python+Tkinter+多线程的实例(python进行多元线性回归模型)原创

随心笔谈2年前发布 编辑
165 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

import Tkinter as tk
from ttk import *
import time
import Queue, threading
class MainWindow:
def __init__(self):
self.root=tk.Tk()
self.root.title(‘Demo’)
def show(self):
self.progress=tk.IntVar()
self.progress_max=100
self.progressbar=Progressbar(self.root, mode=’determinate’, orient=tk.HORIZONTAL, variable=self.progress, maximum=self.progress_max)
self.progressbar.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
self.progress.set(0)
btn=tk.Button(self.root, text=’start’, command=self.start)
btn.pack(fill=tk.BOTH, expand=True, padx=15, pady=5)
self.btn=btn
self.root.mainloop()
def start(self):
self.progress.set(0)
self.btn.config(state=tk.DISABLED)
self.thread_queue=Queue.Queue() # used to communicate between main thread (UI) and worker thread
new_thread=threading.Thread(target=self.run_loop, kwargs={‘param1’:100, ‘param2′:20})
new_thread.start()
# schedule a time-task to check UI
# it’s in main thread, because it’s called by self.root
self.root.after(100, self.listen_for_result)
def run_loop(self, param1, param2):
progress=0
for entry in range(self.progress_max):
time.sleep(0.1)
progress=progress + 1
self.thread_queue.put(progress)
def listen_for_result(self):
”’
Check if there is something in the queue.
Must be invoked by self.root to be sure it’s running in main thread
”’
try:
progress=self.thread_queue.get(False)
self.progress.set(progress)
except Queue.Empty: # must exist to avoid trace-back
pass
finally:
if self.progress.get() < self.progressbar[‘maximum’]:
self.root.after(100, self.listen_for_result)
else:
self.btn.config(state=tk.NORMAL)
if __name__==’__main__’:
win=MainWindow()
win.show()

© 版权声明

相关文章