Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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_Tkinter - Fatal编程技术网

Python Tkinter中的实时时钟显示

Python Tkinter中的实时时钟显示,python,tkinter,Python,Tkinter,我想使用Tkinter和时间库创建实时时钟。我创建了一个类,但不知何故我无法解决我的问题 我的代码 from tkinter import * import time root = Tk() class Clock: def __init__(self): self.time1 = '' self.time2 = time.strftime('%H:%M:%S') self.mFrame = Frame() self.

我想使用Tkinter和时间库创建实时时钟。我创建了一个类,但不知何故我无法解决我的问题

我的代码

from tkinter import *

import time

root = Tk()

class Clock:
    def __init__(self):
        self.time1 = ''
        self.time2 = time.strftime('%H:%M:%S')
        self.mFrame = Frame()
        self.mFrame.pack(side=TOP,expand=YES,fill=X)
        self.watch = Label (self.mFrame, text=self.time2, font=('times',12,'bold'))
        self.watch.pack()
        self.watch.after(200,self.time2)

obj1 = Clock()
root.mainloop()
的第二个参数应该是一个函数-当您提供any时-但您提供的是str对象。因此,您将得到一个错误

from tkinter import *    
import time

root = Tk()

class Clock:
    def __init__(self):
        self.time1 = ''
        self.time2 = time.strftime('%H:%M:%S')
        self.mFrame = Frame()
        self.mFrame.pack(side=TOP,expand=YES,fill=X)

        self.watch = Label(self.mFrame, text=self.time2, font=('times',12,'bold'))
        self.watch.pack()

        self.changeLabel() #first call it manually

    def changeLabel(self): 
        self.time2 = time.strftime('%H:%M:%S')
        self.watch.configure(text=self.time2)
        self.mFrame.after(200, self.changeLabel) #it'll call itself continuously

obj1 = Clock()
root.mainloop()
还请注意:

对该方法的每次调用只调用一次回调。保存 调用回调时,需要在内部重新注册回调 本身


您应该提供您得到的stacktrace,或者描述您面临的问题。您是否收到错误消息?请把它寄出去好吗?