123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- # The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
- # License: http://creativecommons.org/licenses/by-sa/3.0/
- import matplotlib
- from matplotlib.backends.backend_tkagg import (
- FigureCanvasTkAgg, NavigationToolbar2Tk)
- from matplotlib.figure import Figure
- import matplotlib.animation as animation
- from matplotlib import style
- import tkinter as tk
- import serial
- import tk_tools
- import threading
- import queue
- from time import *
- LARGE_FONT= ("Verdana", 12)
- style.use("ggplot")
- #initialize serial port
- ser = serial.Serial()
- ser.port = '/dev/ttyUSB0' #Arduino serial port
- ser.baudrate = 9600
- ser.timeout = 10 #specify timeout when using readline()
- ser.open()
- if ser.is_open==True:
- print("\nAll right, serial port now open. Configuration:\n")
- print(ser, "\n") #print serial parameters
- fig = Figure()
- ax = fig.add_subplot(111)
- xs = [] #store trials here (n)
- ys = [] #store relative frequency here
- class getdata(threading.Thread):
- def __init__(self, label, t, q):
- threading.Thread.__init__(self)
- self.jobqueue = q # get job queue for sending commands
- self.t = t
-
- def run(self):
- while True:# and self.mb.entrycget(1,"label")=="Stop":
- sleep(0.1) # wait a second
- SerialObj = Serial()
- self.t = SerialObj.animate(self)
- # send to job queue !!!
- self.jobqueue.put(("cnt_label",self.t))
- class Serial(tk.Tk):
- def __init__(self):
- tk.Frame.__init__(self)
- def animate(i,self):
- line=ser.readline() #ascii
- line_as_list = line.split(b',')
- i = int(line_as_list[0])
- relProb = line_as_list[1]
- relProb_as_list = relProb.split(b'\n')
- relProb_float = float(relProb_as_list[0])
- # Add x and y to lists
- xs.append(i)
- ys.append(relProb_float)
- ax.clear()
- ax.plot(xs, ys, "#00A3E0", label="1. Graph")
- ax.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2, borderaxespad=0)
- ax.set_title('Windkanal')
- return relProb_float
-
- class Main(tk.Tk):
- def __init__(self, *args, **kwargs):
- tk.Tk.__init__(self, *args, **kwargs)
- tk.Tk.wm_title(self, "Windkanal-Tool")
-
-
- container = tk.Frame(self)
- container.pack(side="top", fill="both", expand = True)
- container.grid_rowconfigure(0, weight=1)
- container.grid_columnconfigure(0, weight=1)
- menubar = tk.Menu(container)
- filemenu = tk.Menu(menubar, tearoff=0)
- filemenu.add_command(label="Save settings", command = lambda: self.popupmsg("Not supported just yet!"))
- filemenu.add_separator()
- filemenu.add_command(label="Exit", command=quit)
- menubar.add_cascade(label="File", menu=filemenu)
- tk.Tk.config(self, menu=menubar)
- self.frames = {}
- for F in (Page_1, Page_2, Page_3, Page_4):
- frame = F(container, self)
- self.frames[F] = frame
- frame.grid(row=0, column=0, sticky="nsew")
- self.show_frame(Page_1)
- def show_frame(self, cont):
- frame = self.frames[cont]
- frame.tkraise()
- def popupmsg(self, msg=""):
- popup = tk.Toplevel(self.master)
- popup.wm_title("Error!")
- label = tk.Label(popup, text=msg, font=LARGE_FONT)
- label.pack(side="top", fill="x", pady=10)
- b1 = tk.Button(popup, text="Okay", command=popup.destroy)
- b1.pack()
- class Page_4(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- label = tk.Label(self, text="Einstellungen", font=LARGE_FONT)
- label.pack(pady=10,padx=10)
- button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
- button1.pack()
- button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
- button2.pack()
- class Page_3(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- label = tk.Label(self, text="Druck", font=LARGE_FONT)
- label.pack(pady=10,padx=10)
- button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
- button1.pack()
- button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
- button2.pack()
- class Page_2(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- label = tk.Label(self, text="Kräfte", font=LARGE_FONT)
- label.pack(pady=10,padx=10)
- button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
- button1.pack()
- button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
- button2.pack()
- class Page_1(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.t = 0
- label = tk.Label(self, text="Bedienelemente", font=LARGE_FONT)
- label.pack(pady=10,padx=10)
- # top menu
- top = tk.Frame(self, borderwidth=2, relief="solid")
- button1 = tk.Button(top, text="Bedienelemente", command=lambda: controller.show_frame(Page_0))
- button1.pack(side=tk.LEFT)
- button2 = tk.Button(top, text="Kräfte", command=lambda: controller.show_frame(Page_2))
- button2.pack(side=tk.LEFT)
- button3 = tk.Button(top, text="Druck", command=lambda: controller.show_frame(Page_2))
- button3.pack(side=tk.LEFT)
- button4 = tk.Button(top,text="Einstellungen",command=lambda: controller.show_frame(Page_2))
- button4.pack(side=tk.LEFT)
- button5 = tk.Button(top, text="QUIT", fg="red",command=quit)
- button5.pack(side=tk.LEFT)
- top.pack(side="top", expand=True, fill="both")
- # graph
- canvas = FigureCanvasTkAgg(fig, self)
- canvas.draw()
- canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- canvas._tkcanvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- # right menu
- left = tk.Frame(self, borderwidth=2, relief="solid")
- container = tk.Frame(left, borderwidth=2, relief="solid")
- label1 = tk.Label(container, text="I could be a canvas, but I'm a label right now")
- SerialObj = Serial()
- #p = tk_tools.RotaryScale(self, max_value=22.0, unit='m/s')
- #p.pack()
- #p.set_value(SerialObj.animate(self))
- #print(SerialObj.animate(self))
- self.seconds = SerialObj.animate(self)
- self.label4 = tk.Label(self,font=("Arial","30"),fg="red")
- self.label4.pack()
- self.label4.config(text=str(self.t))
- self.q = queue.Queue() # Make job queue (Queue)
- offswitch = threading.Event() # Make offswitch (Event)
-
- #self.label4.after(100,self.label_update)
-
- cd = getdata(self.label4, self.t, self.q)
- cd.start()
- SendButton = tk.Button(left, text='Quit', command=quit)
- label2 = tk.Label(left, text="I could be a button")
- label3 = tk.Label(left, text="So could I")
- left.pack(side="left", expand=True, fill="both")
- container.pack(expand=True, fill="both", padx=7, pady=5)
- SendButton.pack()
- label1.pack()
- label2.pack()
- label3.pack()
- def label_update(self):
- job = self.q.get() #get job form queue!!!
-
- if job[0] == "cnt_label":
- self.label4.config(text=str(job[1]) + " Nm")
- else:
- print("Unknown job:", job)
-
- self.label4.after(100,self.label_update)
- def sendFactorToMCU(self):
- self.serialReference.sendSerialData(self.entry.get() + '%')
- app = Main()
- app.geometry("1280x720")
- SerialObj = Serial()
- ani = animation.FuncAnimation(fig, SerialObj.animate, interval=500)
- app.mainloop()
|