# 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()