tkinter_windkanal.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
  2. # License: http://creativecommons.org/licenses/by-sa/3.0/
  3. import matplotlib
  4. from matplotlib.backends.backend_tkagg import (
  5. FigureCanvasTkAgg, NavigationToolbar2Tk)
  6. from matplotlib.figure import Figure
  7. import matplotlib.animation as animation
  8. from matplotlib import style
  9. import tkinter as tk
  10. import serial
  11. import tk_tools
  12. import threading
  13. import queue
  14. from time import *
  15. LARGE_FONT= ("Verdana", 12)
  16. style.use("ggplot")
  17. #initialize serial port
  18. ser = serial.Serial()
  19. ser.port = '/dev/ttyUSB0' #Arduino serial port
  20. ser.baudrate = 9600
  21. ser.timeout = 10 #specify timeout when using readline()
  22. ser.open()
  23. if ser.is_open==True:
  24. print("\nAll right, serial port now open. Configuration:\n")
  25. print(ser, "\n") #print serial parameters
  26. fig = Figure()
  27. ax = fig.add_subplot(111)
  28. xs = [] #store trials here (n)
  29. ys = [] #store relative frequency here
  30. class getdata(threading.Thread):
  31. def __init__(self, label, t, q):
  32. threading.Thread.__init__(self)
  33. self.jobqueue = q # get job queue for sending commands
  34. self.t = t
  35. def run(self):
  36. while True:# and self.mb.entrycget(1,"label")=="Stop":
  37. sleep(0.1) # wait a second
  38. SerialObj = Serial()
  39. self.t = SerialObj.animate(self)
  40. # send to job queue !!!
  41. self.jobqueue.put(("cnt_label",self.t))
  42. class Serial(tk.Tk):
  43. def __init__(self):
  44. tk.Frame.__init__(self)
  45. def animate(i,self):
  46. line=ser.readline() #ascii
  47. line_as_list = line.split(b',')
  48. i = int(line_as_list[0])
  49. relProb = line_as_list[1]
  50. relProb_as_list = relProb.split(b'\n')
  51. relProb_float = float(relProb_as_list[0])
  52. # Add x and y to lists
  53. xs.append(i)
  54. ys.append(relProb_float)
  55. ax.clear()
  56. ax.plot(xs, ys, "#00A3E0", label="1. Graph")
  57. ax.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2, borderaxespad=0)
  58. ax.set_title('Windkanal')
  59. return relProb_float
  60. class Main(tk.Tk):
  61. def __init__(self, *args, **kwargs):
  62. tk.Tk.__init__(self, *args, **kwargs)
  63. tk.Tk.wm_title(self, "Windkanal-Tool")
  64. container = tk.Frame(self)
  65. container.pack(side="top", fill="both", expand = True)
  66. container.grid_rowconfigure(0, weight=1)
  67. container.grid_columnconfigure(0, weight=1)
  68. menubar = tk.Menu(container)
  69. filemenu = tk.Menu(menubar, tearoff=0)
  70. filemenu.add_command(label="Save settings", command = lambda: self.popupmsg("Not supported just yet!"))
  71. filemenu.add_separator()
  72. filemenu.add_command(label="Exit", command=quit)
  73. menubar.add_cascade(label="File", menu=filemenu)
  74. tk.Tk.config(self, menu=menubar)
  75. self.frames = {}
  76. for F in (Page_1, Page_2, Page_3, Page_4):
  77. frame = F(container, self)
  78. self.frames[F] = frame
  79. frame.grid(row=0, column=0, sticky="nsew")
  80. self.show_frame(Page_1)
  81. def show_frame(self, cont):
  82. frame = self.frames[cont]
  83. frame.tkraise()
  84. def popupmsg(self, msg=""):
  85. popup = tk.Toplevel(self.master)
  86. popup.wm_title("Error!")
  87. label = tk.Label(popup, text=msg, font=LARGE_FONT)
  88. label.pack(side="top", fill="x", pady=10)
  89. b1 = tk.Button(popup, text="Okay", command=popup.destroy)
  90. b1.pack()
  91. class Page_4(tk.Frame):
  92. def __init__(self, parent, controller):
  93. tk.Frame.__init__(self, parent)
  94. label = tk.Label(self, text="Einstellungen", font=LARGE_FONT)
  95. label.pack(pady=10,padx=10)
  96. button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
  97. button1.pack()
  98. button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
  99. button2.pack()
  100. class Page_3(tk.Frame):
  101. def __init__(self, parent, controller):
  102. tk.Frame.__init__(self, parent)
  103. label = tk.Label(self, text="Druck", font=LARGE_FONT)
  104. label.pack(pady=10,padx=10)
  105. button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
  106. button1.pack()
  107. button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
  108. button2.pack()
  109. class Page_2(tk.Frame):
  110. def __init__(self, parent, controller):
  111. tk.Frame.__init__(self, parent)
  112. label = tk.Label(self, text="Kräfte", font=LARGE_FONT)
  113. label.pack(pady=10,padx=10)
  114. button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
  115. button1.pack()
  116. button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
  117. button2.pack()
  118. class Page_1(tk.Frame):
  119. def __init__(self, parent, controller):
  120. tk.Frame.__init__(self, parent)
  121. self.t = 0
  122. label = tk.Label(self, text="Bedienelemente", font=LARGE_FONT)
  123. label.pack(pady=10,padx=10)
  124. # top menu
  125. top = tk.Frame(self, borderwidth=2, relief="solid")
  126. button1 = tk.Button(top, text="Bedienelemente", command=lambda: controller.show_frame(Page_0))
  127. button1.pack(side=tk.LEFT)
  128. button2 = tk.Button(top, text="Kräfte", command=lambda: controller.show_frame(Page_2))
  129. button2.pack(side=tk.LEFT)
  130. button3 = tk.Button(top, text="Druck", command=lambda: controller.show_frame(Page_2))
  131. button3.pack(side=tk.LEFT)
  132. button4 = tk.Button(top,text="Einstellungen",command=lambda: controller.show_frame(Page_2))
  133. button4.pack(side=tk.LEFT)
  134. button5 = tk.Button(top, text="QUIT", fg="red",command=quit)
  135. button5.pack(side=tk.LEFT)
  136. top.pack(side="top", expand=True, fill="both")
  137. # graph
  138. canvas = FigureCanvasTkAgg(fig, self)
  139. canvas.draw()
  140. canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  141. canvas._tkcanvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  142. # right menu
  143. left = tk.Frame(self, borderwidth=2, relief="solid")
  144. container = tk.Frame(left, borderwidth=2, relief="solid")
  145. label1 = tk.Label(container, text="I could be a canvas, but I'm a label right now")
  146. SerialObj = Serial()
  147. #p = tk_tools.RotaryScale(self, max_value=22.0, unit='m/s')
  148. #p.pack()
  149. #p.set_value(SerialObj.animate(self))
  150. #print(SerialObj.animate(self))
  151. self.seconds = SerialObj.animate(self)
  152. self.label4 = tk.Label(self,font=("Arial","30"),fg="red")
  153. self.label4.pack()
  154. self.label4.config(text=str(self.t))
  155. self.q = queue.Queue() # Make job queue (Queue)
  156. offswitch = threading.Event() # Make offswitch (Event)
  157. #self.label4.after(100,self.label_update)
  158. cd = getdata(self.label4, self.t, self.q)
  159. cd.start()
  160. SendButton = tk.Button(left, text='Quit', command=quit)
  161. label2 = tk.Label(left, text="I could be a button")
  162. label3 = tk.Label(left, text="So could I")
  163. left.pack(side="left", expand=True, fill="both")
  164. container.pack(expand=True, fill="both", padx=7, pady=5)
  165. SendButton.pack()
  166. label1.pack()
  167. label2.pack()
  168. label3.pack()
  169. def label_update(self):
  170. job = self.q.get() #get job form queue!!!
  171. if job[0] == "cnt_label":
  172. self.label4.config(text=str(job[1]) + " Nm")
  173. else:
  174. print("Unknown job:", job)
  175. self.label4.after(100,self.label_update)
  176. def sendFactorToMCU(self):
  177. self.serialReference.sendSerialData(self.entry.get() + '%')
  178. app = Main()
  179. app.geometry("1280x720")
  180. SerialObj = Serial()
  181. ani = animation.FuncAnimation(fig, SerialObj.animate, interval=500)
  182. app.mainloop()