我有以下代码:
from Tkinter import *
from urllib import urlretrieve
import webbrowser
import ttk
def get_latest_launcher():
webbrowser.open("johndoe.com/get_latest")
global percent
percent = 0
def report(count, blockSize, totalSize):
percent += int(count*blockSize*100/totalSize)
homepage = "http://Johndoe.com"
root = Tk()
root.title("Future of Wars launcher")
Button(text="get latest version", command=get_latest_launcher).pack()
global mpb
mpb = ttk.Progressbar(root, orient="horizontal", variable = percent,length="100",
mode="determinate")
mpb.pack()
root.mainloop()
urlretrieve("https://~url~to~my~file.com",
"Smyprogram.exe",reporthook=report)
但是,如果我运行这个脚本,它将不显示progressbar,而只显示按钮。它甚至不会下载文件,光标就会闪烁。但是,如果我关闭gui窗口,将得到以下代码:
Traceback(most recent call last):
File "C:\Users\user\Desktop\downloader.py", line 28 in <module>
mpb = ttk.Progressbar(root, orient="horizontal", variable =
percent,length="100",mode="determinate")
File "C:\Users/user\Desktop\pyttk-0.3\ttk.py" line 1047, in __init__
Widget.__init__(self, master, "ttk::progressbar", kw)
File "C:\Users/user\Desktop\pyttk-0.3\ttk.py", line 574, in __init__
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 1930, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: this isn't a Tk applicationNULL main window
怎么了?
您的代码中至少有两个问题,但这两个问题都不会导致您所说的错误。
首先,使用普通的python变量作为进度条的
variable
属性的值。虽然这会起作用,但不会像你期望的那样起作用。您需要创建一个tkinterStringVar
或IntVar
的实例。另外,您需要调用该实例的set
方法,以便progressbar看到更改。其次,在调用
mainloop
之后不应该有代码。Tkinter被设计为在mainloop
退出时终止(通常只有在销毁窗口后才会发生)。您需要将呼叫转移到urlretrieve
其他地方。variable = percent
是错误的。您必须使用Tkinter变量作为对象。例如IntVar。相关问题 更多 >
编程相关推荐