P
P
Program_Danil2018-10-17 19:52:54
Python
Program_Danil, 2018-10-17 19:52:54

Where is the problem in the code?

Hello. I'm making a stopwatch in python using the tkinter library. Tell me who knows why 1 code with a formatted string does not work, and 2 code without formatting works?
Code 1:

from tkinter import *
from datetime import datetime

temp = 0
after_id = ''

def tick():
  global temp, after_id
  after_id = root.after(1000, tick)
  f_temp = datetime.fromtimestamp(temp).strftime("%M:%S")
  label1.configure(text=str(f_temp))
  temp += 1

def start_sw():
  btn1.grid_forget()
  btn2.grid(row=1, columnspan=2, sticky='ew')
  tick()

root = Tk()

root.title("Stopwatch")

label1 = Label(root, width=5, font=("Ubuntu", 100), text="00:00")
label1.grid(row=0, columnspan=2)

btn1 = Button(root, text="Start", font=("Ubuntu", 30),command=start_sw)
btn2 = Button(root, text="Stop", font=("Ubuntu", 30))
btn3 = Button(root, text="Continue", font=("Ubuntu", 30))
btn4 = Button(root, text="Reset", font=("Ubuntu", 30))

btn1.grid(row=1, columnspan=2, sticky="ew")

root.mainloop()

Code 2:
from tkinter import *
from datetime import datetime

temp = 0
after_id = ''

def tick():
  global temp, after_id
  after_id = root.after(1000, tick)
  label1.configure(text=str(temp))
  temp += 1

def start_sw():
  btn1.grid_forget()
  btn2.grid(row=1, columnspan=2, sticky='ew')
  tick()

root = Tk()

root.title("Stopwatch")

label1 = Label(root, width=5, font=("Ubuntu", 100), text="00:00")
label1.grid(row=0, columnspan=2)

btn1 = Button(root, text="Start", font=("Ubuntu", 30),command=start_sw)
btn2 = Button(root, text="Stop", font=("Ubuntu", 30))
btn3 = Button(root, text="Continue", font=("Ubuntu", 30))
btn4 = Button(root, text="Reset", font=("Ubuntu", 30))

btn1.grid(row=1, columnspan=2, sticky="ew")

root.mainloop()

The difference in codes occurs on lines 10-11 in the tick() function

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-10-17
@Program_Danil

from datetime import timedelta

f_temp = str(timedelta(seconds=temp))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question