Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新Tkinter窗口的内容_Python_Python 2.7_User Interface_Time_Tkinter - Fatal编程技术网

Python 更新Tkinter窗口的内容

Python 更新Tkinter窗口的内容,python,python-2.7,user-interface,time,tkinter,Python,Python 2.7,User Interface,Time,Tkinter,我正在创建一个Python程序,在Tkinter窗口中显示时间和天气。我需要有时间,天气和任何其他不断更新。这是我的旧代码: import time from Tkinter import * root = Tk() while True: now = time.localtime(time.time()) # Fetch the time label = time.strftime("%I:%M", now) # Format it nicely # We'll a

我正在创建一个Python程序,在Tkinter窗口中显示时间和天气。我需要有时间,天气和任何其他不断更新。这是我的旧代码:

import time

from Tkinter import *

root = Tk()
while True:
    now = time.localtime(time.time()) # Fetch the time
    label = time.strftime("%I:%M", now) # Format it nicely
    # We'll add weather later once we find a source (urllib maybe?)
    w = Label(root, text=label) # Make our Tkinter label
    w.pack()
    root.mainloop()
我以前从未用Tkinter做过任何事情,循环不起作用令人沮丧。显然,Tkinter不允许您在运行时执行任何类似循环或非Tkinter的操作。我想我可以用线程做点什么

#!/usr/bin/env python


# Import anything we feel like importing
import threading
import time

# Thread for updating the date and weather
class TimeThread ( threading.Thread ):
    def run ( self ):
        while True:
            now = time.localtime(time.time()) # Get the time
            label = time.strftime("%I:%M", now) # Put it in a nice format
            global label # Make our label available to the TkinterThread class
            time.sleep(6)
            label = "Weather is unavailable." # We'll add in weather via urllib later.
            time.sleep(6)

# Thread for Tkinter UI
class TkinterThread ( threading.Thread ):
    def run ( self ):
        from Tkinter import * # Import Tkinter
        root = Tk() # Make our root widget
        w = Label(root, text=label) # Put our time and weather into a Tkinter label
        w.pack() # Pack our Tkinter window
        root.mainloop() # Make it go!

# Now that we've defined our threads, we can actually do something interesting.
TimeThread().start() # Start our time thread
while True:
    TkinterThread().start() # Start our Tkinter window
    TimeThread().start() # Update the time
    time.sleep(3) # Wait 3 seconds and update our Tkinter interface
所以这也不起作用。多个空窗口出现,它们发出大量故障。我的调试器中也会出现大量错误

更新时是否需要停止并重新打开窗口?我能告诉Tkinter用
Tkinter.update(root)
之类的东西更新吗

是否有解决方法或解决方案,或者我遗漏了什么?如果你发现我的代码有什么问题,请告诉我

谢谢! Alex

您可以在调用后“嵌套”您的

def update():
    now = time.localtime(time.time())
    label = time.strftime("%I:%M:%S", now)
    w.configure(text=label)
    root.after(1000, update)

现在,您只需在主循环之前在
之后调用一次
,它从现在起每隔一秒就会更新一次。

所以我得到了这个:[link]()它每隔一秒就会打开新的空白窗口。我错过了什么。每次调用
update
时,您都在创建一个新的
Tk
实例。如前所述,将其移到函数外部。2.启动主循环后,不必调用
update
。3.当然,我没有发布整个剧本。我刚才给了您实现功能的方法和一些说明。