gpt4 book ai didi

Python/Glade/PyGTK - Glade 标签中的下载百分比

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:00 25 4
gpt4 key购买 nike

我正在尝试使用 Glade、PyGTK 和 Python 设置一个小的下载应用程序。我已经计划好了我想如何编写这个进度条和所有的脚本,尽管我觉得我正在以错误的方式去做,因为......好吧......它不起作用。

我正在使用我发现的一个小脚本来检查下载的百分比,然后它通常会打印到终端,但我希望它能更改 Glade 中的标签。不过,随着下载的进行,标签不会改变,GUI 会卡住,直到文件完成,然后更新说它已 100% 完成。

制作这样的作品的最佳方法是什么?感谢任何帮助,提前致谢!

Python 脚本:

import sys
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import urllib2
import time

class FileSelector:

def __init__(self):

#Set the Glade file
filename = "FileSelector.glade"
self.builder = gtk.Builder()
self.builder.add_from_file(filename)
self.builder.connect_signals(self)
self.window = self.builder.get_object("MainWindow")
self.label1 = self.builder.get_object("label1")

def on_MainWindow_destroy(self, obj):
gtk.main_quit() #make the program quit


def btnSubmit_clicked(self, widget):
status_dropbox = self.builder.get_object("chkboxDropbox")
status_python = self.builder.get_object("chkboxPython")
status_chrome = self.builder.get_object("chkboxChrome")

#Start downloader script
url = "http://dl.dropbox.com/u/9235267/project.png"

file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)

file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break

file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
self.label1.set_text(status)

f.close()


if __name__ == "__main__":
FileSelector()
gtk.main()

空地文件 (FileSelector.glade):

<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="MainWindow">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">MainWindow</property>
<property name="resizable">False</property>
<property name="window_position">center</property>
<signal name="destroy" handler="on_MainWindow_destroy" swapped="no"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Enviroment Downloader</property>
<attributes>
<attribute name="style" value="normal"/>
<attribute name="size" value="300"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxDropbox">
<property name="label" translatable="yes">Dropbox</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxPython">
<property name="label" translatable="yes">Python</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxChrome">
<property name="label" translatable="yes">Google Chrome</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnSubmit">
<property name="label" translatable="yes">Download/Run</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="btnSubmit_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

最佳答案

行后

self.label1.set_text(status)

添加:

while gtk.events_pending():
gtk.main_iteration()

这将在您等待文件下载时更新 GUI 并运行主循环,并且您不必使用线程。

关于Python/Glade/PyGTK - Glade 标签中的下载百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8148729/

25 4 0