Python Tkinter消息更新时间

Python Tkinter消息更新时间,python,time,tkinter,Python,Time,Tkinter,我想显示显示消息时的时间。我做了,但时间不会更新。它将保留为我启动主循环时的时间。下面我附上了我的代码。我将感谢任何形式的帮助,我可以得到,因为我是一个noob试图学习Python。谢谢 from tkinter import * import time time3 = time.strftime('%H:%M:%S') def tick(): global time1 time2 = time.strftime('%H:%M:

我想显示显示消息时的时间。我做了,但时间不会更新。它将保留为我启动主循环时的时间。下面我附上了我的代码。我将感谢任何形式的帮助,我可以得到,因为我是一个noob试图学习Python。谢谢

    from tkinter import *
    import time

    time3 = time.strftime('%H:%M:%S')

    def tick():
        global time1
        time2 = time.strftime('%H:%M:%S')
        clock.config(text=time2)
        clock.after(200, tick)

    root = Tk()
    root.title("Test GUI")
    time1 = ' '


    def newfile():
        message = (time3 + ':' + "Creating new file..... \n")
        text.insert(0.0, message)

    def openfile():
        message = (time3 + ':' + "Opening existing file..... \n")
        text.insert(0.0, message)

    def starttest():
        message = (time3 + ':' + "Start testing \n")
        text.insert(0.0, message)

    def stoptest():
        message = (time3 + ':' + "Stop testing \n")
        text.insert(0.0, message)

    topFrame = Frame(root)
    topFrame.pack(side = TOP)
    bottomFrame = Frame(root)
    bottomFrame.pack(side = BOTTOM)

    clock = Label(root, font=('times', 10, 'bold'), bd = 1, relief = SUNKEN, anchor = E)
    but1 = Button(topFrame, text = "START", command = starttest) 
    but2 = Button(topFrame, text = "STOP", command = stoptest)
    text = Text(topFrame, width = 35, height = 5, wrap = WORD)

    clock.pack(side = BOTTOM, fill = X)
    but1.grid(row = 3, column = 3)
    but2.grid(row = 3, column = 4)
    text.grid(row = 1, column = 0, columnspan =2, sticky = W)

    menu = Menu(topFrame)
    root.config(menu = menu)

    subMenu = Menu(menu)
    menu.add_cascade(label = "File", menu = subMenu)
    subMenu.add_command(label = "New File", command = newfile)
    subMenu.add_command(label = "Open File", command = openfile)  

    tick()
    root.mainloop()  

首先,我建议您将应用程序作为一个类编写(请参阅),因为这样会更有条理

class Timer(tk.Frame): 
    def __init__(self, parent): # create the time frame...
        self.parent = parent
        self.time_1 = " "
        self.time_label = tk.Label(text=self.time_1)
        self.time_label.pack()
        self.random_lbl = tk.Label(text="hi lol")
        self.random_lbl.pack()
        self.update_clock() # we start the clock here.
更新时间时,请坚持使用该时间使用的变量(例如,在本例中,它是time_1):


勾选
时出现缩进问题,我们无法判断其结束位置。另外,在
tk
对象上缺少一个
mainloop
调用。还有,什么是
文本
?它在任何地方都没有定义。你为什么有时使用
time1
time2
操作,有时使用
time3
操作?你做过研究吗?在这个网站上有很多关于倒计时和使用tkinter的时钟的问题。嗨,我很抱歉,我应该包括我现在做的所有代码。我做了一些研究,我发现问题应该出现在时间3上,它应该包含更多的代码行来实时更新它,但是我被绊住了。如果有人能给我一些建议,我将不胜感激。再次感谢。快速指针:要修改时间戳消息,只需添加
+“blah blah blah”+“spam ham”…
等等。我想问一下,按下按钮时是否可以打印出当前时间。我尝试了你提到的方法,但现在每当我尝试在我的显示框上打印超时时,它总是在同一时间显示。
    def update_clock(self): # update the clock...
        self.time_2 = time.strftime("%H:%M:%S")
        self.time_1 = self.time_2 # we are changing time_1 here.
        self.time_label.config(text=self.time_1)
        self.parent.after(200, self.update_clock) # then we redo the process.

root = tk.Tk() # create the root window.
timer = Timer(root)
root.mainloop()