##### [Tkinter] ClockIn/Out tkinter problem
 [Tkinter] ClockIn/Out tkinter problem Maryan Silly Frenchman Posts: 44 Threads: 22 Joined: Sep 2020 Reputation: Oct-09-2020, 08:58 PM (This post was last modified: Oct-09-2020, 10:10 PM by Yoriz.) I will appreciate any help. I got stuck with hours to find the solution. ```import tkinter as tk import time import datetime from tkinter import Frame from tkinter import * import csv class StopWatch(Frame): def __init__(self, parent=None, **kw): Frame.__init__(self, parent, kw) self._start = 0.0 self._elapsedtime = 0.0 self._running = 0 self.timestr = StringVar() self.makeWidgets() def makeWidgets(self): """ Make the time label. """ self.time_label = Label(self, textvariable=self.timestr) self._setTime(self._elapsedtime) self.time_label.pack(fill=X, expand=NO, pady=2, padx=2) def _update(self): """ Update the label with elapsed time. """ self._elapsedtime = time.time() - self._start self._setTime(self._elapsedtime) self._timer = self.after(50, self._update) def _setTime(self, elap): """ Set the time string to Minutes:Seconds:Hundreths """ minutes = int(elap/60) seconds = int(elap - minutes*60.0) hseconds = int((elap - minutes*60.0 - seconds)*100) self.time_label.configure(text='%02d:%02d:%02d' % (minutes, seconds, hseconds)) def Start(self): """ Start the stopwatch, ignore if running. """ if not self._running: self._start = time.time() - self._elapsedtime self._update() self._running = 1 def Stop(self): """ Stop the stopwatch, ignore if stopped. """ if self._running: self.after_cancel(self._timer) self._elapsedtime = time.time() - self._start self._setTime(self._elapsedtime) self._running = 0 def Reset(self): """ Reset the stopwatch. """ self._start = time.time() self._elapsedtime = 0.0 self._setTime(self._elapsedtime) def clock_in(self): global e1 data = e1.get() ClockIn_time = datetime.datetime.now() ClockIn_date = datetime.datetime.now().strftime('%Y-%m-%d') totalinput = [ data, ClockIn_time, ClockIn_date] with open(self.filename, "a") as savedb: w = csv.writer(savedb) w.writerow(totalinput) def clock_out(self): global e1 data = e1.get() ClockOut_time = datetime.datetime.now() ClockOut_date = datetime.datetime.now().strftime('%Y-%m-%d') totalinput = [ data, ClockOut_time , ClockOut_date ] with open(self.filename, "a") as savedb: w = csv.writer(savedb) w.writerow(totalinput) def main(): global e1 root = Tk() sw = StopWatch(root) sw.pack(side=TOP) root.geometry("400x400") #### label = tk.Label(root, text="Employee Name: ") label.pack(side="top") new = StringVar() e1 = Entry(root, textvariable=new) e1.pack() ####### btn1 = Button(root, text='Clock In', command=lambda :[sw.Start(), sw.clock_in()]) btn1.pack(side=LEFT) btn2 = Button(root, text='Clock Out', command=lambda :[sw.Start(), sw.clock_out()]) btn2.pack(side=LEFT) btn3 = Button(root, text='Reset', command=sw.Reset) btn3.pack(side=LEFT) btn4 = Button(root, text='Quit', command=root.quit) btn4.pack(side=LEFT) root.mainloop() if __name__ == '__main__': main()``` Reply deanhystad So-and-so of the Yard Posts: 2,120 Threads: 11 Joined: Feb 2020 Reputation: Oct-09-2020, 10:40 PM What is the problem? When I press the "Clock In" button I get an error. ``````Error:Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "...\musings\junk.py", line 94, in btn1 = Button(root, text='Clock In', command=lambda :[sw.Start(), sw.clock_in()]) File "...\musings\junk.py", line 64, in clock_in with open(self.filename, "a") as savedb: AttributeError: 'StopWatch' object has no attribute 'filename'``````But this cannot be the problem you are having because it has such an obvious cause. You never specify a value for self.filename, so of course you will get an attribute error when you try to get something that is not an attribute of self. So I commented out where the code writes the file with no name an instead had the code do this. ``` totalinput = [ data, ClockIn_time, ClockIn_date] print(totalinput) ## with open(self.filename, "a") as savedb: ## w = csv.writer(savedb) ## w.writerow(totalinput)```I did the same for clock_out(). Now when I press the Clock In and Clock Out buttons I get this. ``````Output:['Jack', datetime.datetime(2020, 10, 9, 17, 33, 53, 33651), '2020-10-09'] ['Jack', datetime.datetime(2020, 10, 9, 17, 33, 58, 648473), '2020-10-09']``````I entered 'Jack' as the employee name. Is this what you want written to the unnamed file? Looks about right to me. To fix your problem you need to somewhere in you code do this: `self.filename = 'some string'`If the particular problem you are having is something else, you need to provide more information. Reply joe_momma Splitter Posts: 158 Threads: 7 Joined: Nov 2018 Reputation: Oct-12-2020, 03:42 AM Hey, A couple of modifications- added the filename with the suffix .cvs, moved your entry and label into the make widgets method changed the timer update method to show the building of time. removed the lambda in the main. You have functions lambda is for lazy people that don't write functions( they have their place). ```  import tkinter as tk import time import datetime from tkinter import Frame from tkinter import * import csv class StopWatch(Frame):                                                                     def __init__(self, parent=None, **kw):                 Frame.__init__(self, parent, kw)         self._start = 0.0                 self._elapsedtime = 0.0         self._running = 0         self.timestr = StringVar()                  self.filename= 'work_clock.cvs'         self.makeWidgets()              def makeWidgets(self):                                  """ Make the time label. """         self.time_label = Label(self, textvariable=self.timestr)         self._setTime(self._elapsedtime)         self.time_label.pack(fill=X, expand=NO, pady=2, padx=2)                  self.label = tk.Label(self, text="Employee Name: ")         self.label.pack(side="top")         self.new = StringVar()         self.e1 = Entry(self, textvariable=self.new)         self.e1.pack()     def _update(self):          """ Update the label with elapsed time. """                  self._elapsedtime = time.time() - self._start         self._setTime(self._elapsedtime)         self._timer = self.after(50, self._update)     def _setTime(self, elap):         """ Set the time string to Minutes:Seconds:Hundreths """                  minutes = int(elap/60)         seconds = int(elap - minutes*60.0)         hseconds = int((elap - minutes*60.0 - seconds)*100)                         #self.time_label.configure(text='%02d:%02d:%02d' % (minutes, seconds, hseconds))         self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))     def Start(self):                                                              """ Start the stopwatch, ignore if running. """         if not self._running:                         self._start = time.time() - self._elapsedtime             self._update()             self._running = 1             def Stop(self):                                             """ Stop the stopwatch, ignore if stopped. """         if self._running:             self.after_cancel(self._timer)                         self._elapsedtime = time.time() - self._start                 self._setTime(self._elapsedtime)             self._running = 0     def Reset(self):                                           """ Reset the stopwatch. """         self._start = time.time()                  self._elapsedtime = 0.0             self._setTime(self._elapsedtime)     def clock_in(self):         self.Start()                              data = self.e1.get()         ClockIn_time = datetime.datetime.now()         ClockIn_date = datetime.datetime.now().strftime('%Y-%m-%d')         totalinput = [ data, ClockIn_time, ClockIn_date]          with open(self.filename, "a") as savedb:             w = csv.writer(savedb)             w.writerow(totalinput)     def clock_out(self):         self.Stop()                               data = self.e1.get()         ClockOut_time = datetime.datetime.now()         ClockOut_date = datetime.datetime.now().strftime('%Y-%m-%d')         totalinput = [ data, ClockOut_time , ClockOut_date ]          with open(self.filename, "a") as savedb:             w = csv.writer(savedb)             w.writerow(totalinput) def main():         root = Tk()     sw = StopWatch(root)     sw.pack(side=TOP)     root.geometry("400x400")     btn1 = Button(root, text='Clock In', command= sw.clock_in)     btn1.pack(side=LEFT)     btn2 = Button(root, text='Clock Out', command= sw.clock_out)     btn2.pack(side=LEFT)     btn3 = Button(root, text='Reset', command=sw.Reset)     btn3.pack(side=LEFT)     btn4 = Button(root, text='Quit', command=root.quit)     btn4.pack(side=LEFT)     root.mainloop() if __name__ == '__main__':     main()```Now what's left:  this timer is only set up for 59 minutes which would be great if a job only lasted that long, so you'll need to add some hours but that shouldn't be to difficult.  You may need a check to see if there's a name in the entry widget. Maybe set up the cvs with columns and titles to make easy to read. Clean up the data written to cvs so accounting doesn't need a CS to decipher it. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Python3 tkinter radiobutton problem Nick_tkinter 14 1,046 Feb-15-2021, 11:01 PM Last Post: Nick_tkinter tkinter python button position problem Nick_tkinter 3 793 Jan-31-2021, 05:15 AM Last Post: deanhystad tkinter| listbox.insert problem Maryan 3 777 Sep-29-2020, 05:34 PM Last Post: Yoriz Tkinter problem DPaul 6 1,257 May-28-2020, 03:40 PM Last Post: DPaul [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 924 Apr-17-2020, 08:12 PM Last Post: johnjh [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 2,064 Feb-27-2020, 05:17 PM Last Post: Jan_97 [Tkinter] Tkinter problem catlessness 1 814 Jan-15-2020, 05:17 AM Last Post: Larz60+ Problem with Submit button Tkinter Reldaing 2 1,345 Jan-05-2020, 01:58 AM Last Post: balenaucigasa tkinter GUI, problem running seperate files fishglue 17 2,299 Oct-15-2019, 02:56 PM Last Post: Denni [python] [Tkinter] Problem bidding combobox with np.array NEL 3 1,319 Aug-04-2019, 11:07 AM Last Post: Yoriz

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020