Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import tkinter
  4.  
  5. from tkinter import *
  6. import time
  7.  
  8. def graph(file):
  9. f = open(file , "r")
  10. if f.mode == 'r':
  11. contents =f.read()
  12. f1 = contents.split('\n')
  13. gold=0
  14. time = 0
  15. goldList=[]
  16. times=[]
  17. title=0
  18.  
  19. for x in f1:
  20. if time>60:
  21. break
  22. xSplit = x.split(' ')
  23. if len(xSplit) != 1:
  24. gold+= int(xSplit[0])
  25. goldList.append(gold)
  26. timeConversion = xSplit[1].split(":")
  27. timeM = int(timeConversion[0])
  28. timeS = int(timeConversion[1])
  29. timemS = int(timeConversion[2])
  30. t1 = (timeM*60 + timeS + float(timemS))
  31. time += round(t1,2)
  32. times.append(time)
  33. else:
  34. title = xSplit[0]
  35.  
  36. plt.plot(times,goldList,label=title)
  37. plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
  38. ncol=2, mode="expand", borderaxespad=0.)
  39.  
  40. class StopWatch(Frame):
  41. """ Implements a stop watch frame widget. """
  42. def __init__(self, parent=None, **kw):
  43. Frame.__init__(self, parent, kw)
  44. self._start = 0.0
  45. self._elapsedtime = 0.0
  46. self._running = 0
  47. self.timestr = StringVar()
  48. #self.lapstr = StringVar()
  49. self.e = 0
  50. self.m = 0
  51. self.makeWidgets()
  52. self.laps = []
  53. self.lapmod2 = 0
  54. self.today = time.strftime("%d %b %Y %H-%M-%S", time.localtime())
  55.  
  56. def makeWidgets(self):
  57. """ Make the time label. """
  58. l1 = Label(self, text='----Farm Name----')
  59. l1.pack(fill=X, expand=NO, pady=1, padx=2)
  60.  
  61. self.e = Entry(self)
  62. self.e.pack(pady=2, padx=2)
  63.  
  64. j1 = Label(self, text='----Split Value----')
  65. j1.pack(fill=X, expand=NO, pady=1, padx=2)
  66.  
  67. self.p = Entry(self)
  68. self.p.pack(pady=2, padx=2)
  69.  
  70. l = Label(self, textvariable=self.timestr)
  71. self._setTime(self._elapsedtime)
  72. l.pack(fill=X, expand=NO, pady=3, padx=2)
  73.  
  74. l2 = Label(self, text='----Splits----')
  75. l2.pack(fill=X, expand=NO, pady=4, padx=2)
  76.  
  77. scrollbar = Scrollbar(self, orient=VERTICAL)
  78. self.m = Listbox(self,selectmode=EXTENDED, height = 5,
  79. yscrollcommand=scrollbar.set)
  80. self.m.pack(side=LEFT, fill=BOTH, expand=1, pady=5, padx=2)
  81. scrollbar.config(command=self.m.yview)
  82. scrollbar.pack(side=RIGHT, fill=Y)
  83.  
  84. def _update(self):
  85. """ Update the label with elapsed time. """
  86. self._elapsedtime = time.time() - self._start
  87. self._setTime(self._elapsedtime)
  88. self._timer = self.after(50, self._update)
  89.  
  90. def _setTime(self, elap):
  91. """ Set the time string to Minutes:Seconds:Hundreths """
  92. minutes = int(elap/60)
  93. seconds = int(elap - minutes*60.0)
  94. hseconds = int((elap - minutes*60.0 - seconds)*100)
  95. self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
  96.  
  97. def _setLapTime(self, elap):
  98. """ Set the time string to Minutes:Seconds:Hundreths """
  99. minutes = int(elap/60)
  100. seconds = int(elap - minutes*60.0)
  101. hseconds = int((elap - minutes*60.0 - seconds)*100)
  102. return '%02d:%02d:%02d' % (minutes, seconds, hseconds)
  103.  
  104. def Start(self):
  105. """ Start the stopwatch, ignore if running. """
  106. if not self._running:
  107. self._start = time.time() - self._elapsedtime
  108. self._update()
  109. self._running = 1
  110.  
  111. def Stop(self):
  112. """ Stop the stopwatch, ignore if stopped. """
  113. if self._running:
  114. self.after_cancel(self._timer)
  115. self._elapsedtime = time.time() - self._start
  116. self._setTime(self._elapsedtime)
  117. self._running = 0
  118.  
  119. def Reset(self):
  120. """ Reset the stopwatch. """
  121. self._start = time.time()
  122. self._elapsedtime = 0.0
  123. self.laps = []
  124. self._setTime(self._elapsedtime)
  125.  
  126. def Lap(self):
  127. '''Makes a lap, only if started'''
  128. tempo = self._elapsedtime - self.lapmod2
  129. if self._running:
  130. self.laps.append(self._setLapTime(tempo))
  131. self.m.insert(END, self.laps[-1])
  132. self.m.yview_moveto(1)
  133. self.lapmod2 = self._elapsedtime
  134.  
  135. def GravaCSV(self):
  136. '''Pega nome do cronometro e cria arquivo para guardar as laps'''
  137. arquivo = str(self.e.get()) + ' - '
  138. inventoryValue = str(self.p.get())
  139. with open('Routes/' + str(self.e.get()) + '.txt', 'wb') as lapfile:
  140. lapfile.write(bytes(str(self.e.get()) + '\n', 'utf-8'))
  141. for lap in self.laps:
  142. lapfile.write((bytes(inventoryValue + ' ' + str(lap) + '\n', 'utf-8')))
  143. graph('Routes/' + str(self.e.get()) + '.txt')
  144. plt.show()
  145.  
  146. def main():
  147. root = Tk()
  148. root.wm_attributes("-topmost", 1) #always on top - might do a button for it
  149. sw = StopWatch(root)
  150. sw.pack(side=TOP)
  151.  
  152. Button(root, text='Split', command=sw.Lap).pack(side=LEFT)
  153. Button(root, text='Start', command=sw.Start).pack(side=LEFT)
  154. Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
  155. #Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
  156. Button(root, text='Save', command=sw.GravaCSV).pack(side=LEFT)
  157. Button(root, text='Quit', command=root.quit).pack(side=LEFT)
  158.  
  159. root.mainloop()
  160.  
  161. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement