[英]Show extracting a .zip file progress Python Tkinter ProgressBar
我正在开发一个提取zip文件的程序,该部分已经解决了。 但是我想添加一个进度条以显示提取过程的进度。 我已经有一个进度栏,但是它没有显示提取进度。 我希望它从点0开始到点100,这将是提取完成时的终点。 但是取而代之的是,它一直重复从点0到点100重复直到提取完成。 我怎样才能做到这一点?
码:
from threading import Thread
from Tkinter import *
import zipfile
import ttk
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self,parent)
self.pack()
self.create_widgets()
Thread(target=self.startExtr).start()
def create_widgets(self):
self.pBar = ttk.Progressbar(orient=HORIZONTAL, length=200, mode="determinate")
self.pBar.pack(side=BOTTOM)
def startExtr(self):
self.pBar.start()
with zipfile.ZipFile('Kerbal Space Program.zip', "r") as z:
z.extractall("")
self.pBar.stop()
root = Tk()
root.title("Test")
app = Application(root)
root.mainloop()
不幸的是,找到答案后不是很有帮助:(。但是我设法找到一个非常有帮助的例子,并且确实做了我想要的!它得到了提取的百分比:)。 这是我找到它的地方的链接: Monitor ZIP File Extraction Python
这是我的新代码:
from threading import Thread
from Tkinter import *
import zipfile
import time
import ttk
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self,parent)
self.pack()
self.create_widgets()
Thread(target=self.unzip).start()
def create_widgets(self):
self.pBar = ttk.Progressbar(orient=HORIZONTAL, length=200, mode="determinate", maximum=100, value=0)
self.pBar.pack(side=BOTTOM)
def unzip(self):
zf = zipfile.ZipFile('Name of the zip file')
uncompress_size = sum((file.file_size for file in zf.infolist()))
extracted_size = 0
for file in zf.infolist():
extracted_size += file.file_size
percentage = extracted_size * 100/uncompress_size
self.pBar["value"] = percentage
zf.extract(file)
root = Tk()
root.title("Test")
app = Application(root)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.