我有以下代码:
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)它甚至不会下载文件,而且光标只会闪烁。但是,如果我关闭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怎么了?
发布于 2014-06-23 04:34:18
您的代码中至少有两个问题,尽管这两个问题都不会导致您所说的错误。
首先,使用一个普通的python变量作为进度条的variable属性的值。虽然这可以工作,但它不会像您预期的那样工作。您需要创建一个tkinter StringVar或IntVar实例。此外,您还需要调用该实例的set方法,以便进度条看到更改。
其次,在调用mainloop之后,您永远不应该有代码。Tkinter被设计为在mainloop退出时终止(通常只有在销毁窗口后才会发生)。您需要将调用转移到其他地方的urlretrieve。
发布于 2014-06-23 03:44:28
variable = percent错了。您必须使用Tkinter变量,它们是对象。例如IntVar。
https://stackoverflow.com/questions/24349136
复制相似问题