54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
"""
|
|
|
|
使用多线程的情况 - 耗时间的任务在独立的线程中执行
|
|
|
|
Version: 0.1
|
|
Author: 骆昊
|
|
Date: 2018-03-20
|
|
|
|
"""
|
|
|
|
import time
|
|
import tkinter
|
|
import tkinter.messagebox
|
|
from threading import Thread
|
|
|
|
|
|
def main():
|
|
|
|
class DownloadTaskHandler(Thread):
|
|
|
|
def run(self):
|
|
# 模拟下载任务需要花费10秒钟时间
|
|
time.sleep(10)
|
|
tkinter.messagebox.showinfo('提示', '下载完成!')
|
|
# 启用下载按钮
|
|
button1.config(state=tkinter.NORMAL)
|
|
|
|
def download():
|
|
# 禁用下载按钮
|
|
button1.config(state=tkinter.DISABLED)
|
|
# 通过daemon参数将线程设置为守护线程(主程序退出就不再保留执行)
|
|
DownloadTaskHandler(daemon=True).start()
|
|
|
|
def show_about():
|
|
tkinter.messagebox.showinfo('关于', '作者: 骆昊(v1.0)')
|
|
|
|
top = tkinter.Tk()
|
|
top.title('单线程')
|
|
top.geometry('200x150')
|
|
top.wm_attributes('-topmost', 1)
|
|
|
|
panel = tkinter.Frame(top)
|
|
button1 = tkinter.Button(panel, text='下载', command=download)
|
|
button1.pack(side='left')
|
|
button2 = tkinter.Button(panel, text='关于', command=show_about)
|
|
button2.pack(side='right')
|
|
panel.pack(side='bottom')
|
|
|
|
tkinter.mainloop()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|