jackfrued / jackfrued/Python-100-Days

Day 13 进程和线程

Open
#387 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
187k
Forks
55.8k
PR merge metrics
No merged PRs in 30d

Description

应用案例1 的代码:为什么class DownloadTaskHandler 和 Download()函数必须要在main函数里面?
`
import time
import tkinter
import tkinter.messagebox
from threading import Thread

def main():

class DownloadTaskHandler(Thread):

    def run(self):
        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()
`

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the embedded example in issue #387 and trace main(), DownloadTaskHandler, and download() together with Python threading and tkinter references. Determine how the nested definitions relate to the GUI callbacks and document whether that structure is required; done means the scope question is answered clearly for readers of Day 13.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
desktop
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.