【问题标题】:Tkinter -tkinter.TclErrorTkinter -tkinter.TclError
【发布时间】:2014-06-22 07:40:42
【问题描述】:

我有以下代码:

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

怎么了?

【问题讨论】:

  • 此代码适用于 Linux。我使用命令pip install pyttk 安装了 pyttk 0.3.2。 (Python 2.7.5)
  • @furas 真的吗?我正在使用 Windows,并且安装了 ttk,我遇到了什么问题?
  • 我认为 Tkinter 尝试运行 ttk::progressbar 但 Tcl/Tk 找不到它。我看到你在桌面上有 pyttk - 我会尝试使用pip 命令安装它,它应该将它安装在 Python 文件夹中并下载任何依赖项。
  • 您可以尝试运行Tcl/Tk脚本(paste.tclers.tk/2675)来测试Tcl/Tk中是否有ttk::progressbar。你可以运行它tclsh my_script.tcl
  • 您的代码和错误不匹配。错误显示mph = ttk...,您的代码显示mpb = ttk...。虽然我意识到这是一个微小的差异,但它让我想知道是否还有更多差异。此外,您发布的代码运行(不是很好,但它运行)并且不会抛出您所说的错误。

标签: python tkinter progress-bar ttk


【解决方案1】:

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

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

其次,在调用mainloop 之后,您永远不应该有代码。 Tkinter 旨在在 mainloop 退出后终止(这通常仅在您销毁窗口后发生)。您需要将呼叫转移到urlretrieve 其他地方。

【讨论】:

    【解决方案2】:

    variable = percent 是错误的。您必须使用作为对象的 Tkinter 变量。比如 IntVar。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多