1

I am trying to create a Tkinter GUI that records the time-pressed between buttons and then appends the tuple output to a list however it is recording the time difference as 1625044470.590308. I have no idea what this number means so if anybody understands this weird bug help would be appreciated. My Code:

import time
from tkinter import *

initial = 0
top = Tk()
command_time_list = []
master_list = []
print(command_time_list)
#start forward
def start_time_forward():
   global initial
   print("Timer Start")
   initial = time.time()
   return initial

def stop_time_forward():
   final = time.time()
   time_elapsed = final - initial
   command_time_list.append('a')
   command_time_list.append(time_elapsed)
   command_time_tuple = tuple(command_time_list)
   return command_time_tuple

command_time_tuple = stop_time_forward()
def append():
    master_list.append(command_time_tuple)
    print(master_list)

forward_start = Button(top, text ="Forward Time Start", command = start_time_forward)
forward_end = Button(top, text ="Forward Time Stop", command = stop_time_forward)
append = Button(top, text="append path pair", command=append)
Agarb
  • 111
  • 1
  • 8

2 Answers2

0

Pythom time method returns the time as a floating point number expressed in seconds since the epoch, in UTC. your mistake was forgetting global and also initial value mustn't be 0 if so then you will get time from begining of epoch you will have at line 27

command_time_tuple = stop_time_forward() 

time_elapsed = final - 0 where final is time.time() that return the time in seconds since the epoch

command_time_tuple = stop_time_forward() # value at t0

import time
from tkinter import *

initial = time.time() #0
top = Tk()
command_time_list = []
master_list = []
print(command_time_list)
#start forward
def start_time_forward():
   global initial #like you used global here to be able to change global value
   print("Timer Start")
   initial = time.time()
   return initial

def stop_time_forward():
   final = time.time()
   time_elapsed = final - initial
   command_time_list.append('a')
   command_time_list.append(time_elapsed)
   ##!!!!! you must use global here too because  command_time_tuple is global variable !!!!!!!!!
   global command_time_tuple
   
   command_time_tuple = tuple(command_time_list) # value at t_new
   return command_time_tuple

command_time_tuple = stop_time_forward() # value at t0
def append():
    #now each time this function will be run it will append master_list with the new at t_new not with t0
    master_list.append(command_time_tuple)
    print(master_list)

forward_start = Button(top, text ="Forward Time Start", command = start_time_forward)
forward_end = Button(top, text ="Forward Time Stop", command = stop_time_forward)
append = Button(top, text="append path pair", command=append)
append.pack()
forward_end.pack()
forward_start.pack()
Bahae El Hmimdi
  • 364
  • 1
  • 5
0

I believe that this is what you are trying to achive:

import time
from tkinter import *

initial = 0
master_list = []
command_time_list: tuple = tuple()
top = Tk()
top.geometry("600x300")

def start_time_forward():
    global initial
    initial = time.time()
    print("updated")

def stop_time_forward():
    global command_time_list
    time_elapsed = time.time() - initial
    command_time_list = ('a', time_elapsed)


def append():
    global command_time_list
    # Added an if statement to make sure that the user hits the stop button
    if command_time_list:
        master_list.append(command_time_list)
    print(master_list)
    command_time_list = tuple()

forward_start = Button(top, text="Forward Time Start", command=start_time_forward)
forward_end = Button(top, text="Forward Time Stop", command=stop_time_forward)
append = Button(top, text="append path pair", command=append)
forward_start.pack()
forward_end.pack()
append.pack()
top.mainloop()
Omid Ki
  • 155
  • 3
  • 13