博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tkinter threading unblock GUI ( check if the thread exists)
阅读量:6689 次
发布时间:2019-06-25

本文共 2754 字,大约阅读时间需要 9 分钟。

from tkinter import *from tkinter.ttk import *import timeimport threadingimport requestsfrom bs4 import BeautifulSoupurl3 = 'http://stackoverflow.com/questions/31241112/blocking-tkinter-interface-until-thread-finishes-its-task'class Interface:    def __init__(self, master):        self.mutex = threading.Lock()        self.m_list = []        self.master = master        self.browse_button= Button (master, text="Browse", command=self.browser)        self.browse_button.pack()        # Create an indeterminate progressbar here but don't pack it.        # Change the maximum to change speed. Smaller == faster.        self.progressbar = Progressbar(mode="indeterminate", maximum=20)    def browser (self):        self.start = time.time()        # set up thread to do work in        self.thread = threading.Thread(target=self.read_file, args=("filename",))        # disable the button        self.browse_button.config(state="disabled")        # show the progress bar        self.progressbar.pack()        # change the cursor        self.master.config(cursor="wait")        # force Tk to update        self.master.update()        # start the thread and progress bar        self.thread.start()        self.progressbar.start()        # check in 50 milliseconds if the thread has finished        self.master.after(50, self.check_completed)    def check_completed(self):        if self.thread.is_alive():            # if the thread is still alive check again in 50 milliseconds            self.master.after(50, self.check_completed)        else:            # if thread has finished stop and reset everything            self.progressbar.stop()            self.progressbar.pack_forget()            self.browse_button.config(state="enabled")            self.master.config(cursor="")            self.master.update()            # Call method to do rest of work, like displaying the info.            self.display_file()    def read_file (self, filename):        # time.sleep(7)  # actually do the read here        self.mutex.acquire()        try:            resp = requests.get(url3)            soup = BeautifulSoup(resp.content, 'html.parser')            for link in soup.select('a'):                self.m_list.append(link.get('href'))        finally:            self.mutex.release()    def display_file(self):        new_list = []        for i in self.m_list:            # print type(i) # unicode            new_list.append(str(i).strip('\n'))        self.cp(new_list)    def cp(self, new_list):        tp = Toplevel(self.master)        text = Text(tp)        text.pack()        text.insert(1.0, '\n'.join(new_list))        print time.time() - self.startwindow = Tk()starter = Interface(window)window.mainloop()

转载于:https://www.cnblogs.com/otfsenter/p/6562264.html

你可能感兴趣的文章
次世代的会话管理项目 Spring Session
查看>>
SQL SERVER 2008安全配置
查看>>
Json hijacking/Json劫持漏洞
查看>>
算法知识梳理(5) 数组第二部分
查看>>
多页应用增量更新静态资源Webpack打包方案
查看>>
ionic V3.3开发踩坑集锦
查看>>
Realm入门指北
查看>>
彻底搞懂Bean加载
查看>>
iOS之传值
查看>>
技术blog 迁移
查看>>
linux 定时器怎么用? crontab 基础
查看>>
React Native - Image
查看>>
Docker和宿主机操作系统文件目录互相隔离的实现原理
查看>>
小程序踩坑系列一
查看>>
探索webpack热更新对代码打包结果的影响(二)
查看>>
微信小程序_豆瓣电影
查看>>
记一次网络模块的小规模重构
查看>>
FMI-人工智能&大数据高峰论坛(深圳站)
查看>>
区块链简单研读笔记
查看>>
为什么 scrum 开发人员是一个 T-形的人 ?
查看>>