簡體   English   中英

Tkinter -tkinter.TclError

[英]Tkinter -tkinter.TclError

我有以下代碼:

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

怎么了?

variable = percent錯誤。 您必須使用作為對象的Tkinter變量。 如IntVar。

您的代碼中至少有兩個問題,盡管它們都不引起您說的錯誤。

首先,使用普通的python變量作為進度條的variable屬性的值。 盡管這將起作用,但它不會按您預期的那樣工作。 您需要創建tkinter StringVarIntVar的實例。 另外,您需要調用該實例的set方法,以便進度條看到更改。

其次,在調用mainloop之后,您永遠都不會有代碼。 Tkinter設計為在mainloop退出后終止(通常僅在銷毀窗口之后才發生)。 您將需要將調用移到urlretrieve其他位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM