首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter -tkinter.TclError

Tkinter -tkinter.TclError
EN

Stack Overflow用户
提问于 2014-06-22 15:40:43
回答 2查看 12.2K关注 0票数 1

我有以下代码:

代码语言:javascript
复制
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窗口,我会得到以下代码:

代码语言:javascript
复制
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

怎么了?

EN

回答 2

Stack Overflow用户

发布于 2014-06-23 04:34:18

您的代码中至少有两个问题,尽管这两个问题都不会导致您所说的错误。

首先,使用一个普通的python变量作为进度条的variable属性的值。虽然这可以工作,但它不会像您预期的那样工作。您需要创建一个tkinter StringVarIntVar实例。此外,您还需要调用该实例的set方法,以便进度条看到更改。

其次,在调用mainloop之后,您永远不应该有代码。Tkinter被设计为在mainloop退出时终止(通常只有在销毁窗口后才会发生)。您需要将调用转移到其他地方的urlretrieve

票数 2
EN

Stack Overflow用户

发布于 2014-06-23 03:44:28

variable = percent错了。您必须使用Tkinter变量,它们是对象。例如IntVar。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24349136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档