class Screen(Observer): def __init__(self,parent,bg="white"): self.canvas=Canvas(parent,bg=bg) print("parent",parent.cget("width"),parent.cget("height")) parent.bind("<Configure>", self.resize) self.parent=parent self.width=int(self.canvas.cget("width")) self.height=int(self.canvas.cget("height")) self.models=[] def update(self,model): if model not in self.models: self.models.append(model) print("View update") signal=model.get_signal() self.plot_signal(signal,model.get_color(),model.get_name()) def plot_signal(self,signal,color,name): w,h=self.canvas.winfo_width(),self.canvas.winfo_height() width,height=int(w),int(h) print(self.canvas.find_withtag("signal"+name)) if self.canvas.find_withtag("signal"+name) : self.canvas.delete("signal"+name) if signal and len(signal) > 1: if name=="X-Y" : plot = [((x+2)*width/4, (2*y/self.m+1)*height/2) for (x, y) in signal] else : plot = [(x*width, y*height/self.m + height/2) for (x, y) in signal] signal_id = self.canvas.create_line(plot, fill=color, smooth=1, width=3,tags="signal"+name) return def packing(self) : self.canvas.pack(fill="both", expand=1) def grid(self,n,m): self.n=n self.m=m w,h=self.canvas.winfo_width(),self.canvas.winfo_height() self.width,self.height=int(w),int(h) self.canvas.create_line(0,self.height/2.0,self.width-4,self.height/2,arrow="last",tags="line",fill="blue") self.canvas.create_line(self.width/2,self.height,self.width/2,5,arrow="last",tags="line",fill="blue") step1=self.width/n for t in range(1,n): x =t*step1 self.canvas.create_line(x,0,x,self.height,tags="line") step=self.height/m for t in range(1,m): y =t*step self.canvas.create_line(0,y,self.width,y,tags="line") def resize(self,event): self.canvas.delete("line") self.grid(self.n,self.m) for model in self.models : self.plot_signal(model.get_signal(), model.get_color(), model.get_name())
class View(Observer): def __init__(self,parent,subject,bg="white"): print("View : __init__") Observer.__init__(self) self.subject=subject self.parent=parent self.signal_id=None self.canvas=Canvas(parent,bg=bg) self.canvas.bind("<Configure>", self.resize) self.width=int(self.canvas.cget("width")) self.height=int(self.canvas.cget("height")) def update(self,subject): print("View : update") signal=subject.get_signal() self.signal_id=self.plot_signal(signal) def plot_signal(self,signal,color="red"): width,height=self.width,self.height if self.signal_id!=None : self.canvas.delete(self.signal_id) if signal and len(signal)>1: plot=[(x*width, height/2.0*(y+1)) for (x, y) in signal] self.signal_id=self.canvas.create_line(plot,fill=color,smooth=1,width=3) return self.signal_id def resize(self, event): """ En cas de reconfiguration de fenetre """ if event: self.width = event.width self.height = event.height ## self.width = int(event.widget.cget("width")) ## self.height = int(event.widget.cget("height")) ## print("View : resize cget",event.widget.cget("width"),event.widget.cget("height")) print("View : resize event",event.width,event.height) self.canvas.delete("grid") self.plot_signal(self.subject.get_signal()) self.grid() def grid(self, steps=8): width,height=self.width,self.height # self.canvas.create_line(10,height/2,width,height/2,arrow="last",tags="grid") # self.canvas.create_line(10,height-5,10,5,arrow="last",tags="grid") step=width/steps*1. for t in range(1,steps+2): x =t*step self.canvas.create_line(x,0,x,height,tags="grid") self.canvas.create_line(0,x,width,x,tags="grid") self.canvas.create_line(x,height/2-4,x,height/2+4,tags="grid") def packing(self) : self.canvas.pack(expand=1,fill="both",padx=6)
class MainWindow(Tk): def __init__(self, width=100, height=100, bg="red"): Tk.__init__(self) self.title("Editeur Graphique") self.geometry("400x300+1200+300") self.canvas = Canvas(self, width=width - 20, height=height - 20, bg=bg) self.libelle = Label(text="Serious Game", font="Helvetica 14 bold") self.canvas.pack() self.libelle.pack() print(self.canvas.cget("height")) print(self.canvas.cget("width")) print(self.canvas.winfo_height()) print(self.canvas.winfo_width())
class Lissajou(Observer): def __init__(self, parent,subjects,bg="black"): self.subjects=subjects self.canvas=Canvas(parent,bg=bg) self.width = int(self.canvas.cget("width")) self.height = int(self.canvas.cget("height")) self.signalXY_id = None self.canvas.bind("<Configure>", self.resize) self.packing() def update(self): print("Lissajou update") self.signalXY_id = self.plot_lissajou() def resize(self, event): if event: self.width=event.width self.height=event.height self.grid() self.update() def grid(self, n=10, m=10,color="white"): self.canvas.delete("all") w,h=self.width,self.height width,height=int(w),int(h) self.canvas.create_line(n,(height/2.0),width,(height/2.0),arrow="last",fill=color) self.canvas.create_line(width/2.0,height,width/2.0,5.0,arrow="last",fill=color) stepX=(width)/m*1.0 stepY=(height)/n*1.0 for t in range(1,m+1): x =t*stepX self.canvas.create_line(x,height,x,20,fill=color) for t in range(1,n+1): y =t*stepY self.canvas.create_line(10.0,y,width-10,y,fill=color) def plot_lissajou(self,color='green'): width,height=int(self.width-12),int(self.height) signalXY = self.subjects.getSignalXY() if signalXY!=None: self.canvas.delete(self.signalXY_id) if signalXY and len(signalXY)>1: plot=[((x+1)*(width/2)+5, height/2.0*(y+1)) for (x, y) in signalXY] signalValue = self.canvas.create_line(plot, fill=color, smooth=1, width=2) return signalValue def packing(self): self.canvas.pack(expand=1, fill='both',side='top')
class MainWindow(Frame): def __init__(self, parent=None, width=100,height=100,bg="red"): Frame.__init__(self,parent, relief="sunken", bd=5) self.canvas =Canvas(self,width=width-20,height=height-20, bg=bg) self.libelle =Label(text ="Serious Game", font="Helvetica 14 bold") print(self.canvas.cget("height")) print(self.canvas.cget("width")) print(self.canvas.winfo_height()) print(self.canvas.winfo_width()) def packing(self) : self.pack() self.canvas.pack(fill="both",expand=True) self.libelle.pack()
class MainWindow(Frame): def __init__(self, parent=None, bg="red"): Frame.__init__(self, parent, relief="sunken", bd=5) self.parent = parent self.menubar = MenuBar(self) self.canvas = Canvas(self, bg=bg) self.libelle = Label(text="Serious Game", font="Helvetica 14 bold") print(self.canvas.cget("height")) print(self.canvas.cget("width")) print(self.canvas.winfo_height()) print(self.canvas.winfo_width()) def packing(self): self.menubar.pack(fill="x") self.canvas.pack(fill="both", expand=True) self.libelle.pack() self.pack() def file_save(self): formats = [('Texte', '*.py'), ('Portable Network Graphics', '*.png')] if sys.version_info[0] < 3: filename = tkFileDialog.asksaveasfilename(parent=self.parent, filetypes=formats, title="Save...") else: filename = filedialog.asksaveasfilename(parent=self.parent, filetypes=formats, title="Save...") if len(nfilename) > 0: print("Sauvegarde en cours dans %s" % filename) def file_quit(self): exit(0) def create_circle(self): pass def delete_circle(self): pass def help_us(self): pass def help_tkinter(self): pass
class Screen(Observer): def __init__(self,parent,bg="white"): self.canvas=Canvas(parent,bg=bg) print("parent",parent.cget("width"),parent.cget("height")) self.magnitude=Scale(parent,length=250,orient="horizontal", label="Magnitude", sliderlength=20, showvalue=0,from_=0,to=5, tickinterval=25) def update(self,model): print("View update") signal=model.get_signal() self.plot_signal(signal) def get_magnitude(self): return self.magnitude def plot_signal(self,signal,color="red"): w,h=self.canvas.cget("width"),self.canvas.cget("height") width,height=int(w),int(h) # print(self.canvas.find_withtag("signal")) if self.canvas.find_withtag("signal") : self.canvas.delete("signal") if signal and len(signal) > 1: plot = [(x*width, height/2.0*(y+1)) for (x, y) in signal] signal_id = self.canvas.create_line(plot, fill=color, smooth=1, width=3,tags="signal") return def grid(self, steps): w,h=self.canvas.cget("width"),self.canvas.cget("height") width,height=int(w),int(h) self.canvas.create_line(10,height/2,width,height/2,arrow="last") self.canvas.create_line(10,height-5,10,5,arrow="last") step=(width-10)/steps*1. for t in range(1,steps+2): x =t*step self.canvas.create_line(x,height/2-4,x,height/2+4) def packing(self) : self.canvas.pack() self.magnitude.pack()
class Screen(Observer): def __init__(self, parent, model_x, model_y, bg="white"): self.canvas = Canvas(parent, bg=bg) self.model_x = model_x self.model_y = model_y print("parent", parent.cget("width"), parent.cget("height")) self.showX = True self.showY = True self.frame = Frame(parent) # Signal X self.magnitude_x = Scale(self.frame, length=250, orient="horizontal", name="m_x", label="Magnitude X", sliderlength=20, showvalue=0, from_=0, to=5, tickinterval=25) self.frequency_x = Scale(self.frame, length=250, orient="horizontal", name="f_x", label="Frequency X", sliderlength=20, showvalue=0, from_=0, to=5, tickinterval=25) self.phase_x = Scale(self.frame, length=250, orient="horizontal", name="p_x", label="Phase X", sliderlength=20, showvalue=0, from_=0, to=5, tickinterval=25) # Signal Y self.magnitude_y = Scale(self.frame, length=250, orient="horizontal", name="m_y", label="Magnitude Y", sliderlength=20, showvalue=0, from_=0, to=5, tickinterval=25) self.frequency_y = Scale(self.frame, length=250, orient="horizontal", name="f_y", label="Frequency Y", sliderlength=20, showvalue=0, from_=0, to=5, tickinterval=25) self.phase_y = Scale(self.frame, length=250, orient="horizontal", name="p_y", label="Phase Y", sliderlength=20, showvalue=0, from_=0, to=5, tickinterval=25) self.frame2 = Frame(parent, bg="black") self.varX = IntVar() self.varY = IntVar() self.varXY = IntVar() self.lbl = Label(self.frame2, text="Courbes", fg="black") # Boutons de sélection (X, Y ou X-Y) self.caseX = Checkbutton(self.frame2, text="X", variable=self.varX, command=self.getX) self.caseY = Checkbutton(self.frame2, text="Y", variable=self.varY, command=self.getY) self.caseXY = Checkbutton(self.frame2, text="XY", variable=self.varXY, command=self.getXY) self.caseXY.select() self.wi = self.canvas.cget("width") self.hi = self.canvas.cget("height") self.stepx = 0 self.stepy = 0 # Step x self.step_x = Entry(parent, name="x") # Step y self.step_y = Entry(parent, name="y") def update(self, model): print("View update") if model.getId() == 0: signal = model.get_signal() self.plot_signal(signal) elif model.getId() == 1: signal = model.get_signal() self.plot_signal(signal, "blue") else: raise ("Error") # Signal X def get_magnitude(self, whichOne): if whichOne == 0: return self.magnitude_x elif whichOne == 1: return self.magnitude_y else: raise ("Error") def get_frequency(self, whichOne): if whichOne == 0: return self.frequency_x elif whichOne == 1: return self.frequency_y else: raise ("Error") def get_phase(self, whichOne): if whichOne == 0: return self.phase_x elif whichOne == 1: return self.phase_y else: raise ("Error") def get_step_x(self): return self.step_x def get_step_y(self): return self.step_y def getX(self): print("update_X(self,event)") self.caseY.deselect() self.caseXY.deselect() self.showX = True self.showY = False self.update(self.model_x) if self.canvas.find_withtag("signal_y"): self.canvas.delete("signal_y") def getY(self): print("update_Y(self,event)") self.caseX.deselect() self.caseXY.deselect() self.showX = False self.showY = True self.update(self.model_y) if self.canvas.find_withtag("signal_x"): self.canvas.delete("signal_x") def getXY(self): print("update_XY(self,event)") self.caseX.deselect() self.caseY.deselect() self.showX = True self.showY = True self.update(self.model_x) self.update(self.model_y) def plot_signal(self, signal, color="red"): w, h = self.wi, self.hi width, height = int(w), int(h) if color == "red" and self.showX == True: if self.canvas.find_withtag("signal_x"): self.canvas.delete("signal_x") if signal and len(signal) > 1: plot = [(x * width, height / 2.0 * (y + 1)) for (x, y) in signal] signal_id = self.canvas.create_line(plot, fill=color, smooth=1, width=3, tags="signal_x") elif color == "blue" and self.showY == True: if self.canvas.find_withtag("signal_y"): self.canvas.delete("signal_y") if signal and len(signal) > 1: plot = [(x * width, height / 2.0 * (y + 1)) for (x, y) in signal] signal_id = self.canvas.create_line(plot, fill=color, smooth=1, width=3, tags="signal_y") def grid(self, step_x, step_y): w, h = self.wi, self.hi width, height = int(w), int(h) self.stepx = (width - 10) / step_x * 1. self.stepy = (height - 10) / step_y * 1. for t in range(1, step_x + 2): x = t * self.stepx self.canvas.create_line(x, 0, x, height, tags="grid") #self.canvas.create_line(x,height/2-4,x,height/2+4) for t in range(1, step_y + 2): y = t * self.stepy self.canvas.create_line(0, y, width, y, tags="grid") #self.canvas.create_line(width/2-4,y,width/2+4,y) def resize(self, event): if event: self.wi = event.width self.hi = event.height self.canvas.delete("grid") self.plot_signal(self.model_x.get_signal()) self.plot_signal(self.model_y.get_signal(), "blue") self.grid(25, 25) def packing(self): self.canvas.pack(fill="both", expand=1) self.step_x.pack(expand=1, fill="both") self.step_y.pack(expand=1, fill="both") self.frame.pack(expand=1, fill="both") self.magnitude_x.grid(row=0, column=0) self.magnitude_y.grid(row=0, column=1) self.frequency_x.grid(row=1, column=0) self.frequency_y.grid(row=1, column=1) self.phase_x.grid(row=2, column=0) self.phase_y.grid(row=2, column=1) self.frame2.pack(side="bottom", expand=1) self.lbl.grid(row=0, column=0) self.caseX.grid(row=0, column=1) self.caseY.grid(row=0, column=2) self.caseXY.grid(row=0, column=3)
class Screen(Observer): def __init__(self, parent, bg="white"): self.canvas = Canvas(parent, bg=bg) self.canvas.bind("<Configure>", self.resize) self.width = int(self.canvas.cget("width")) self.height = int(self.canvas.cget("height")) print("parent", parent.cget("width"), parent.cget("height")) self.magnitude = Scale(parent, length=250, orient="horizontal", label="Magnitude", sliderlength=20, showvalue=0, from_=0, to=10, tickinterval=1, relief="sunken") self.frequency = Scale(parent, length=250, orient="horizontal", label="Frequency", sliderlength=20, showvalue=0, from_=0, to=10, tickinterval=1, relief="sunken") self.phase = Scale(parent, length=250, orient="horizontal", label="Phase", sliderlength=20, showvalue=0, from_=0, to=10, tickinterval=1, relief="sunken") def update(self, model): print("View update") signal = model.get_signal() self.plot_signal(signal) def get_magnitude(self): return self.magnitude def get_frequency(self): return self.frequency def get_phase(self): return self.phase def plot_signal(self, signal, color="red"): w, h = self.canvas.winfo_width(), self.canvas.winfo_height() width, height = int(w), int(h) print(self.canvas.find_withtag("signal")) if self.canvas.find_withtag("signal"): self.canvas.delete("signal") if signal and len(signal) > 1: plot = [(x * width, height / 2.0 * (y + 1)) for (x, y) in signal] signal_id = self.canvas.create_line(plot, fill=color, smooth=1, width=3, tags="signal") return def grid(self, steps): w, h = self.canvas.cget("width"), self.canvas.cget("height") width, height = int(w), int(h) # self.canvas.create_line(10,height/2,width,height/2,arrow="last") # self.canvas.create_line(10,height-5,10,5,arrow="last") step = (width - 10) / steps * 1. for t in range(1, steps + 2): x = t * step self.canvas.create_line(x, 0, x, height) self.canvas.create_line(0, x, width, x) def packing(self): self.canvas.pack() self.magnitude.pack() self.frequency.pack() self.phase.pack() self.canvas.pack(expand=1, fill="both", padx=6) self.magnitude.pack(expand=1, fill="x", padx=2) self.frequency.pack(expand=1, fill="x", padx=2) self.phase.pack(expand=1, fill="x", padx=2) def resize(self, event): wscale = float(event.width) / self.width hscale = float(event.height) / self.height self.width = event.width self.height = event.height print("resize", wscale, hscale) # resize the canvas #self.config(width=self.width, height=self.height) scale_xy = self.width * 1.0 / self.height # rescale all the objects tagged with the "all" tag self.canvas.scale("all", 0, 0, wscale, hscale)