| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | |
| 19 | class DownloadTaskHandler(Thread): |
| 20 | |
| 21 | def run(self): |
| 22 | # 模拟下载任务需要花费10秒钟时间 |
| 23 | time.sleep(10) |
| 24 | tkinter.messagebox.showinfo('提示', '下载完成!') |
| 25 | # 启用下载按钮 |
| 26 | button1.config(state=tkinter.NORMAL) |
| 27 | |
| 28 | def download(): |
| 29 | # 禁用下载按钮 |
| 30 | button1.config(state=tkinter.DISABLED) |
| 31 | # 通过daemon参数将线程设置为守护线程(主程序退出就不再保留执行) |
| 32 | DownloadTaskHandler(daemon=True).start() |
| 33 | |
| 34 | def show_about(): |
| 35 | tkinter.messagebox.showinfo('关于', '作者: 骆昊(v1.0)') |
| 36 | |
| 37 | top = tkinter.Tk() |
| 38 | top.title('单线程') |
| 39 | top.geometry('200x150') |
| 40 | top.wm_attributes('-topmost', 1) |
| 41 | |
| 42 | panel = tkinter.Frame(top) |
| 43 | button1 = tkinter.Button(panel, text='下载', command=download) |
| 44 | button1.pack(side='left') |
| 45 | button2 = tkinter.Button(panel, text='关于', command=show_about) |
| 46 | button2.pack(side='right') |
| 47 | panel.pack(side='bottom') |
| 48 | |
| 49 | tkinter.mainloop() |
| 50 | |
| 51 | |
| 52 | if __name__ == '__main__': |