不确定progressbar在python tkin中不停止

2024-06-25 22:51:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在pythontkinter中启动和停止progressbar以进行下载过程。我试图为下载进度设置一个线程并启动进程栏。然而,酒吧一直在营业,我得到了一个“如果你自己”的答案,你是不是停了_已启动。是否已设置():RecursionError:超过最大递归深度“错误。你知道吗

代码如下:

import time
import threading
import tkinter
from tkinter import ttk
from six.moves import urllib

def start_thread():
    b_start['state'] = 'disable'
    progbar.start()
    thread = threading.Thread(target=tasks)
    thread.start()
    root.after(50, check_thread(thread))

def check_thread(thread):
    if thread.is_alive():
        root.after(50, check_thread(thread))
    else:
        progbar.stop()
        b_start['state'] = 'normal'

def download_func():
    urllib.request.urlretrieve('ftp://ftp.ebi.ac.uk/pub/databases/uniprot/knowledgebase/uniprot_sprot_varsplic.fasta.gz', 'uni.dat')

def tasks():
    download_func()

root = tkinter.Tk()

progbar = ttk.Progressbar(root)
progbar.config(maximum=8,mode='indeterminate')
progbar.pack()

b_start = ttk.Button(root, text='Start',command=start_thread)
b_start.pack()

root.mainloop()

我做错什么了?!你知道吗

干杯, 卷曲的


Tags: fromimporttkinterdefcheckrooturllibthread
1条回答
网友
1楼 · 发布于 2024-06-25 22:51:24

问题出在函数上

def check_thread(thread):
    if thread.is_alive():
        root.after(50, check_thread(thread))
    else:
        progbar.stop()
        b_start['state'] = 'normal'

事实上

root.after(50, check_thread(thread))

立即重新执行check_thread,导致RecursionErrorafter的正确语法是root.after(<delay>, <function to execute>),因此,这里用

root.after(50, lambda: check_thread(thread))

解决问题。你知道吗

相关问题 更多 >