-1

I want to display the time when I am displaying my message. I did it but the time would not be updated. It will remain as the time when I started the mainloop. Below I attached my code. I will appreciate any form of help I could get as I am a noob trying to learn Python. Thanks.

    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()  
S.G. Harmonia
  • 297
  • 2
  • 18
M.Hui
  • 3
  • 2
  • 2
    There's an indentation problem with `tick`, we can't tell where it ends. Also, your missing a `mainloop` call on your `tk` object. Also, what's `text`? It's not defined anywhere. Why are you operating on `time1` and `time2` sometimes, and sometimes `time3`? – kabanus Jul 03 '17 at 09:14
  • Have you done any research? There are many questions on this site related to countdown timers and clocks using tkinter. – Bryan Oakley Jul 03 '17 at 11:46
  • Hi I am so sorry I should have included all the code which I have did now. I did some research I kinda figure out that the problem should be at time3 which should include a few more lines of code to update it real time but I am stucked. Would appreciate anyone giving me ideas. Thanks once again. – M.Hui Jul 03 '17 at 18:18

1 Answers1

0

Firstly, I would recommend you write your application as a class (see Best way to structure a tkinter application) as it will be more organized.

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.

When you update the time, stick to the variable you used for the time (say, in this case, it's time_1):

    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()
S.G. Harmonia
  • 297
  • 2
  • 18
  • Quick pointer: to modify the timestamp message, just add `+ "blah blah blah" + "spam ham" ...` and so on. – S.G. Harmonia Jul 04 '17 at 21:11
  • I would like to ask if it is possible to print out the currently time when the button is pressed down. I tried the method you mention but now whenever i try to print the time out on my display box it keeps on showing the same time. – M.Hui Jul 05 '17 at 02:53