python-为什么tkinter进度栏会使事情变得这么慢?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-为什么tkinter进度栏会使事情变得这么慢?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4425字,纯文字阅读大概需要7分钟。
内容图文
![python-为什么tkinter进度栏会使事情变得这么慢?](/upload/InfoBanner/zyjiaocheng/688/c861b2f04f9046ebbc5dbfb27a7178a2.jpg)
我有以下代码用于提取tar.gz文件,同时保留进度标签:
from __future__ import division
import tarfile
import os
theArchive = "/Users/Dennis/Instances/atlassian-jira-enterprise-4.1.2-standalone.tar.gz"
a = tarfile.open(theArchive)
tarsize = 0
print "Computing total size"
for tarinfo in a:
tarsize = tarsize + tarinfo.size
realz = tarsize
print "compressed size: " + str(a.fileobj.size)
print "uncompressed size: " + str(tarsize)
tarsize = 0
for tarinfo in a:
print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
if tarinfo.isreg():
print "a regular file."
elif tarinfo.isdir():
print "a directory."
else:
print "something else."
a.extract(tarinfo)
tarsize = tarsize + tarinfo.size
print str(tarsize) + "/" + str(realz)
outout = tarsize / realz
print "progress: " + str(outout)
a.close()
这非常快,可以在10秒内提取100MB的tar.gz.我也希望在视觉上具有此功能,因此我将其更改为包括tkinter进度栏:
from __future__ import division
import tarfile
import os
import Tkinter
class Meter(Tkinter.Frame):
def __init__(self, master, width=300, height=20, bg='white', fillcolor='orchid1',\
value=0.0, text=None, font=None, textcolor='black', *args, **kw):
Tkinter.Frame.__init__(self, master, bg=bg, width=width, height=height, *args, **kw)
self._value = value
self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
highlightthickness=0, relief='flat', bd=0)
self._canv.pack(fill='both', expand=1)
self._rect = self._canv.create_rectangle(0, 0, 0, self._canv.winfo_reqheight(), fill=fillcolor,\
width=0)
self._text = self._canv.create_text(self._canv.winfo_reqwidth()/2, self._canv.winfo_reqheight()/2,\
text='', fill=textcolor)
if font:
self._canv.itemconfigure(self._text, font=font)
self.set(value, text)
self.bind('<Configure>', self._update_coords)
def _update_coords(self, event):
'''Updates the position of the text and rectangle inside the canvas when the size of
the widget gets changed.'''
# looks like we have to call update_idletasks() twice to make sure
# to get the results we expect
self._canv.update_idletasks()
self._canv.coords(self._text, self._canv.winfo_width()/2, self._canv.winfo_height()/2)
self._canv.coords(self._rect, 0, 0, self._canv.winfo_width()*self._value, self._canv.winfo_height())
self._canv.update_idletasks()
def get(self):
return self._value, self._canv.itemcget(self._text, 'text')
def set(self, value=0.0, text=None):
#make the value failsafe:
if value < 0.0:
value = 0.0
elif value > 1.0:
value = 1.0
self._value = value
if text == None:
#if no text is specified use the default percentage string:
text = "Extraction: " + str(int(round(100 * value))) + ' %'
self._canv.coords(self._rect, 0, 0, self._canv.winfo_width()*value, self._canv.winfo_height())
self._canv.itemconfigure(self._text, text=text)
self._canv.update_idletasks()
##-------------demo code--------------------------------------------##
def _goExtract(meter, value):
meter.set(value)
if value < 1.0:
value = value + 0.005
meter.after(50, lambda: _demo(meter, value))
else:
meter.set(value, 'Demo successfully finished')
if __name__ == '__main__':
root = Tkinter.Tk(className='meter demo')
m = Meter(root, relief='ridge', bd=3)
m.pack(fill='x')
m.set(0.0, 'Computing file size...')
m.after(1000)
theArchive = "/Users/Dennis/Instances/atlassian-jira-enterprise-4.1.2-standalone.tar.gz"
a = tarfile.open(theArchive)
tarsize = 0
for tarinfo in a:
tarsize = tarsize + tarinfo.size
realz = tarsize
print "real size: " + str(tarsize)
print "compressed size: " + str(a.fileobj.size)
m.set(0.0, 'Done computing!')
m.after(1000)
tarsize = 0
for tarinfo in a:
print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
if tarinfo.isreg():
print "a regular file."
elif tarinfo.isdir():
print "a directory."
else:
print "something else."
a.extract(tarinfo)
tarsize = tarsize + tarinfo.size
print str(tarsize) + "/" + str(realz)
outout = tarsize / realz
m.set(outout)
print "progress: " + str(outout)
a.close()
m.set(1.0, 'Extraction complete!')
m.after(1000)
m.after(1000, lambda: _goExtract(m, 0.0))
一切正常,但现在过程需要2分钟多的时间.为什么会发生这种情况,我该如何解决?
谢谢!
丹尼斯
解决方法:
档案中的文件有多大?几乎可以肯定,更新进度条的工作量远远超出了您的需要-通常在set()函数中包含一个检查,以便如果从上一个值开始的更改太小,它只返回而不进行更新.对于300px的画布,绝对没有必要进行小于0.3%的更改,并且更新频率可能不超过每1%.
由于您的过程通常在10秒钟内完成,因此您可能还需要引入基于时间的检查,因为即使每1%更新一次,每秒也会进行10次,这超出了您的需要.如果您从简单的for循环中驱动Tk,绘制条会花费多长时间会很有趣.
内容总结
以上是互联网集市为您收集整理的python-为什么tkinter进度栏会使事情变得这么慢?全部内容,希望文章能够帮你解决python-为什么tkinter进度栏会使事情变得这么慢?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。