简体   繁体   English

ttk进度条冻结

[英]ttk progress bar freezing

I want a progress bar that shows the user the download progress. 我想要一个向用户显示下载进度的进度条。 When updating the GUI and downloading at the same time the progress bar freezes, and I understand why but I don't know how to solve it. 当更新GUI并同时下载进度条冻结时,我理解为什么但我不知道如何解决它。 I tried multithreading using this post: Tkinter: How to use threads to preventing main event loop from “freezing” and using The Basics of Python Multithreading and Queues as a guid to help me fit it to my needs. 我尝试使用这篇文章进行多线程处理: Tkinter:如何使用线程来防止主事件循环“冻结”并使用Python多线程和队列的基础知识来帮助我满足我的需求。 The problem is that which way I try to achieve my goal, I always seem to make a mistake when changing it to do what I need it to do. 问题是我尝试实现目标的方式,在更改它以执行我需要它做的事情时,我似乎总是犯了一个错误。

The most basic version of my code (without multithreading): 我的代码的最基本版本(没有多线程):

from Tkinter import *
import ttk
from urllib import URLopener # Downloading files 

# Make frame to tell user what file is getting downloaded
self.Progressmsg = Label(self, text="TempValue")
self.Progressmsg.pack(pady=(10,0))

# Make progress bar to show user download progress
self.Progressbar = ttk.Progressbar(self, mode="determinate", orient='horizontal', lengt=280, maximum=len(self.AllClasses))
self.Progressbar.pack(padx=10, pady=10)
self.Progressbar["value"] = 0

def DownloadFile(Class):
    # Update progress message
    self.Progressmsg["text"] = "Downloading {0}.ics...".format(Class)

    # Download each file from saxion website
    CalFile = URLopener()
    CalFile.retrieve("http://[school website]/ical/group/{0}.ics".format(Class), "Data/{0}.ics".format(Class))

    # Update progress bar
    self.Progressbar["value"] += 1

for Study in self.Parameters["Classes"]:
    for Class in Study:
        DownloadFile(Class)

Notes: In this code AllClasses is a list of different classes from which a calendar file has to be downloaded. 注意:在此代码中, AllClasses是必须从中下载日历文件的不同类的列表。
The code itself is part of a fairly large class which I didn't include. 代码本身是一个相当大的类的一部分,我没有包含它。 This is why I am using self.[variablename] 这就是我使用self的原因。[variablename]

When this code runs the progressbar doesn't load or update, all the files download properly and when they are downloaded the progress bar updates everything at once. 当此代码运行时,进度条不会加载或更新,所有文件都会正确下载,当它们下载时,进度条会立即更新所有内容。 My question is: how do I solve this problem in my case? 我的问题是:在我的案例中如何解决这个问题?

Try this: 尝试这个:

# Update progress bar
self.Progressbar["value"] += 1
self.Progressbar.update_idletasks()

If it does not work then use self.Progressbar.update() instead. 如果它不起作用,那么请改用self.Progressbar.update()

The GUI won't reflect your changes if there is something else to do (like downloading the next file) unless you call update_idletasks() or update() . 除非您调用update_idletasks()update()否则如果还有其他事情要做(如下载下一个文件),GUI将不会反映您的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM