How can I measure elapsed time between the press of buttons in python with tkinter?
Question: I'm very new to python and I'm trying to do a little project for myself, but I
don't understand how to use the initial time variable from the start function
in the stop function where I can do math with it. This is my code currently:

    
    
    import time
    import Tkinter
    import tkMessageBox
    
    top = Tkinter.Tk()
    
    def start_time():
       tkMessageBox.showinfo("Timer", "The timer will now begin")
       initial = time.time()
       return initial
    
    def stop_time(initial):
       final = time.time()
       tkMessageBox.showinfo("Timer", final - initial)
    
    Start = Tkinter.Button(top, text ="Start", command = start_time)
    Stop = Tkinter.Button(top, text ="Stop", command = stop_time)
    Start.pack()
    Stop.pack()
    top.mainloop()
    


Answer: Your functions need to agree on a common place for shared data. For this
simple example, the module's global namespace is a good choice. All you need
to do is add `global initial` to the functions that update it. For larger
projects you may move to objects that hold the variables and the functions
that update it, but this is fine for your goals.

    
    
    import time
    import Tkinter
    import tkMessageBox
    
    initial = 0
    
    top = Tkinter.Tk()
    
    def start_time():
       global initial
       tkMessageBox.showinfo("Timer", "The timer will now begin")
       initial = time.time()
       return initial
    
    def stop_time():
       # you could check for initial == 0 and display an error
       final = time.time()
       tkMessageBox.showinfo("Timer", final - initial)
    
    Start = Tkinter.Button(top, text ="Start", command = start_time)
    Stop = Tkinter.Button(top, text ="Stop", command = stop_time)
    Start.pack()
    Stop.pack()
    top.mainloop()
    

