Skip to content
Snippets Groups Projects
Commit 5e1f77f9 authored by Jdrezen's avatar Jdrezen
Browse files

Merge branch 'master' into window_closing

parents 15dc4b42 dec394be
Branches
No related tags found
No related merge requests found
from time import sleep
class Amas:
def __init__(self):
self.agents = []
self.sleep = 1
self.__observer = None
def attach(self,observer):
self.__observer = observer
def notifyObserver(self,value):
self.__observer.updateCycle(value)
def add_agent(self,name):
self.agents.append(name)
def remove_agent(self):
del self.agents[len(self.agents)-1]
def reset(self):
self.agents = []
def set_speed(self,value):
self.sleep = value
def cycle(self):
while (True):
for i in range (len(self.agents)):
print('Je suis le numéro %d' % i)
sleep(10 / self.sleep)
self.notifyObserver(self.sleep)
...@@ -57,6 +57,12 @@ class Controleur: ...@@ -57,6 +57,12 @@ class Controleur:
def draw_image(self, x : float, y : float, name : str) -> int: def draw_image(self, x : float, y : float, name : str) -> int:
return self.__fenetre.draw_image(x, y, name) return self.__fenetre.draw_image(x, y, name)
"""
Draw a text with x,y coords
"""
def draw_text(self, x : float, y : float, text : str) -> int:
return self.__fenetre.draw_text(x,y,text)
""" """
Move an image to x,y coords Move an image to x,y coords
""" """
...@@ -106,18 +112,54 @@ class Controleur: ...@@ -106,18 +112,54 @@ class Controleur:
def addBarChart(self, name : str) -> int: def addBarChart(self, name : str) -> int:
return self.__fenetre.addBarChart(name) return self.__fenetre.addBarChart(name)
"""
Set the color to the bar chart identified by id
"""
def setColor(self, id : int, color : str) -> None: def setColor(self, id : int, color : str) -> None:
self.__fenetre.setColor(id, color) self.__fenetre.setColor(id, color)
"""
Add a plot chart to the window
"""
def addPlotChart(self, name : str) -> int:
return self.__fenetre.addPlotChart(name)
"""
Set the drawing policy to the curve of the plot chart identified by idCurve and id
"""
def setPolicy(self, id : int, idCurve : int, policy : str) -> None:
self.__fenetre.setPolicy(id, idCurve, policy)
"""
Set the title to the chart identified by id
"""
def setTitle(self, id : int, name : str) -> None: def setTitle(self, id : int, name : str) -> None:
self.__fenetre.setTitle(id, name) self.__fenetre.setTitle(id, name)
"""
Set the label on the x axis to the chart identified by id
"""
def setXLabel(self, id : int, name : str) -> None: def setXLabel(self, id : int, name : str) -> None:
self.__fenetre.setXLabel(id, name) self.__fenetre.setXLabel(id, name)
"""
Set the label on the y axis to the chart identified by id
"""
def setYLabel(self, id : int, name : str) -> None: def setYLabel(self, id : int, name : str) -> None:
self.__fenetre.setYLabel(id, name) self.__fenetre.setYLabel(id, name)
"""
Add a curve to the plot with the given policy
"""
def addCurve(self, id : int, policy : str) -> None:
self.__fenetre.addCurve(id,policy)
"""
Add a point to the plot with x,y coords
"""
def addPoint(self, id : int, id_curve : int, x : float, y : float) -> None:
self.__fenetre.addPoint(id,id_curve,x,y)
""" """
Add a column at the end of the figure Add a column at the end of the figure
""" """
...@@ -166,7 +208,6 @@ class Controleur: ...@@ -166,7 +208,6 @@ class Controleur:
Reset amas Reset amas
""" """
def updateReset(self) -> None: def updateReset(self) -> None:
#self.remove_all()
self.__amas.reset() self.__amas.reset()
print("Reset de la simulation") print("Reset de la simulation")
...@@ -196,11 +237,14 @@ class Controleur: ...@@ -196,11 +237,14 @@ class Controleur:
def updateWindow(env,amas): def updateWindow(env,amas):
pass pass
def initialisation(self): def __initialisation(self) -> None:
pass pass
def start(self): """
self.initialisation() Launch the amas in a thread and display the window
"""
def start(self) -> None:
self.__initialisation()
self.__th = Thread(target=self.__amas.start) self.__th = Thread(target=self.__amas.start)
self.__th.start() self.__th.start()
self.__fenetre.display() self.__fenetre.display()
......
...@@ -9,7 +9,8 @@ sys.path.insert(0, str(pathlib.Path(__file__).parent)) ...@@ -9,7 +9,8 @@ sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakIHM.classes.panelCommandes import PanelCommandes from pyAmakIHM.classes.panelCommandes import PanelCommandes
from pyAmakIHM.classes.panelVue import PanelVue from pyAmakIHM.classes.panelVue import PanelVue
from pyAmakIHM.classes.panelGraphique import PanelGraphique from pyAmakIHM.classes.panelBarChart import PanelBarChart
from pyAmakIHM.classes.panelPlotChart import PanelPlotChart
from tkinter import ttk, Tk from tkinter import ttk, Tk
...@@ -86,6 +87,12 @@ class Fenetre : ...@@ -86,6 +87,12 @@ class Fenetre :
def draw_image(self, x : float, y : float, name : str) -> int: def draw_image(self, x : float, y : float, name : str) -> int:
return self.__panelVue.draw_image(x,y,name) return self.__panelVue.draw_image(x,y,name)
"""
Draw a text with x,y coords
"""
def draw_text(self, x : float, y : float, text : str) -> int:
return self.__panelVue.draw_text(x,y,text)
""" """
Move an image to x,y coords Move an image to x,y coords
""" """
...@@ -133,24 +140,65 @@ class Fenetre : ...@@ -133,24 +140,65 @@ class Fenetre :
""" """
def addBarChart(self, name : str) -> int: def addBarChart(self, name : str) -> int:
length = len(self.__panelGraphiques) length = len(self.__panelGraphiques)
self.__panelGraphiques.append(PanelGraphique(self.__onglet,length)) self.__panelGraphiques.append(PanelBarChart(self.__onglet,length))
self.__panelGraphiques[length].attach(self) self.__panelGraphiques[length].attach(self)
self.__onglet.add(self.__panelGraphiques[length],text = name) self.__onglet.add(self.__panelGraphiques[length],text = name)
return (length) return (length)
"""
Set the color to the bar chart identified by id
"""
def setColor(self, id : int, color : str) -> None: def setColor(self, id : int, color : str) -> None:
self.__panelGraphiques[id].setColor(color) self.__panelGraphiques[id].setColor(color)
"""
Add a plot chart to the window
"""
def addPlotChart(self, name : str) -> int:
length = len(self.__panelGraphiques)
self.__panelGraphiques.append(PanelPlotChart(self.__onglet,length))
self.__panelGraphiques[length].attach(self)
self.__onglet.add(self.__panelGraphiques[length],text = name)
return (length)
"""
Set the drawing policy to the curve of the plot chart identified by idCurve and id
"""
def setPolicy(self, id : int, idCurve, policy : str) -> None:
self.__panelGraphiques[id].setPolicy(idCurve,policy)
"""
Set the title to the chart identified by id
"""
def setTitle(self, id : int, name : str) -> None: def setTitle(self, id : int, name : str) -> None:
self.__panelGraphiques[id].setTitle(name) self.__panelGraphiques[id].setTitle(name)
"""
Set the label on the x axis to the chart identified by id
"""
def setXLabel(self, id : int, name : str) -> None: def setXLabel(self, id : int, name : str) -> None:
self.__panelGraphiques[id].setXLabel(name) self.__panelGraphiques[id].setXLabel(name)
"""
Set the label on the y axis to the chart identified by id
"""
def setYLabel(self, id : int, name : str) -> None: def setYLabel(self, id : int, name : str) -> None:
self.__panelGraphiques[id].setYLabel(name) self.__panelGraphiques[id].setYLabel(name)
"""
Add a curve to the plot
"""
def addCurve(self, id : int, policy : str) -> None:
self.__panelGraphiques[id].addCurve(policy)
"""
Add a point to the plot with x,y coords
"""
def addPoint(self, id : int, id_curve : int, x : float, y : float) -> None:
self.__panelGraphiques[id].addPoint(id_curve,x,y)
""" """
Add a column at the end of the figure Add a column at the end of the figure
""" """
...@@ -190,7 +238,7 @@ class Fenetre : ...@@ -190,7 +238,7 @@ class Fenetre :
""" """
Bind the mouse's button release when a tab is dragged Bind the mouse's button release when a tab is dragged
""" """
def is_drag(self, event : 'event'): def is_drag(self, event : 'event') -> None:
self.__root.config(cursor="fleur") self.__root.config(cursor="fleur")
self.__onglet.bind('<ButtonRelease>',self.tab_drop) self.__onglet.bind('<ButtonRelease>',self.tab_drop)
...@@ -216,7 +264,10 @@ class Fenetre : ...@@ -216,7 +264,10 @@ class Fenetre :
self.__panelGraphiques[id] = panel self.__panelGraphiques[id] = panel
self.__onglet.add(self.__panelGraphiques[id]) self.__onglet.add(self.__panelGraphiques[id])
def on_closing(self): """
Notify the controleur to stop the app and close the window
"""
def on_closing(self) -> None:
self.__observer.updateClosing() self.__observer.updateClosing()
for pan in self.__panelGraphiques: for pan in self.__panelGraphiques:
pan.quit() pan.quit()
......
""" """
Class PanelGraphique Class PanelBarChart
""" """
from tkinter import Frame, Toplevel from tkinter import Frame, Toplevel
from matplotlib.figure import Figure from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class PanelGraphique(Frame): class PanelBarChart(Frame):
""" """
Class PanelGraphique Class PanelBarChart
""" """
def __init__( def __init__(
...@@ -25,6 +25,7 @@ class PanelGraphique(Frame): ...@@ -25,6 +25,7 @@ class PanelGraphique(Frame):
self.__axis = self.__fig.add_subplot() self.__axis = self.__fig.add_subplot()
self.__xAxis = [] self.__xAxis = []
self.__yAxis = [] self.__yAxis = []
self.__axis.bar(self.__xAxis,self.__yAxis) self.__axis.bar(self.__xAxis,self.__yAxis)
self.__graphique = FigureCanvasTkAgg(self.__fig,master = self) self.__graphique = FigureCanvasTkAgg(self.__fig,master = self)
...@@ -49,24 +50,36 @@ class PanelGraphique(Frame): ...@@ -49,24 +50,36 @@ class PanelGraphique(Frame):
""" """
Notify the observer about an event Notify the observer about an event
""" """
def notify(self, panel : 'PanelGraphique', id : int) -> None: def notify(self, panel : 'PanelBarChart', id : int) -> None:
self.__observer.updateGraphique(panel,id) self.__observer.updateGraphique(panel,id)
"""
Set the color to the bar chart
"""
def setColor(self, color : str) -> None: def setColor(self, color : str) -> None:
self.__axis.clear() self.__axis.clear()
self.__color = color self.__color = color
self.__rebuild() self.__rebuild()
"""
Set the title to the chart
"""
def setTitle(self, name : str) -> None: def setTitle(self, name : str) -> None:
self.__axis.clear() self.__axis.clear()
self.__title = name self.__title = name
self.__rebuild() self.__rebuild()
"""
Set the label on the x axis to the chart
"""
def setXLabel(self, name : str) -> None: def setXLabel(self, name : str) -> None:
self.__axis.clear() self.__axis.clear()
self.__XLabel = name self.__XLabel = name
self.__rebuild() self.__rebuild()
"""
Set the label on the y axis to the chart
"""
def setYLabel(self, name : str) -> None: def setYLabel(self, name : str) -> None:
self.__axis.clear() self.__axis.clear()
self.__YLabel = name self.__YLabel = name
...@@ -127,11 +140,11 @@ class PanelGraphique(Frame): ...@@ -127,11 +140,11 @@ class PanelGraphique(Frame):
""" """
Return a copy of the object Return a copy of the object
""" """
def createCopy(self, name : str, id : int): def createCopy(self, name : str, id : int) -> 'PanelBarChart':
window = Toplevel() window = Toplevel()
window.title(name) window.title(name)
self.__copy = PanelGraphique(window, id) self.__copy = PanelBarChart(window, id)
self.__copy.pack(fill='both',expand='yes') self.__copy.pack(fill='both',expand='yes')
self.__copyWidget() self.__copyWidget()
...@@ -149,7 +162,7 @@ class PanelGraphique(Frame): ...@@ -149,7 +162,7 @@ class PanelGraphique(Frame):
""" """
Copy the object Copy the object
""" """
def __copyWidget(self): def __copyWidget(self) -> None:
self.__copy.__id = self.__id self.__copy.__id = self.__id
self.__copy.__xAxis = self.__xAxis self.__copy.__xAxis = self.__xAxis
self.__copy.__yAxis = self.__yAxis self.__copy.__yAxis = self.__yAxis
...@@ -158,9 +171,9 @@ class PanelGraphique(Frame): ...@@ -158,9 +171,9 @@ class PanelGraphique(Frame):
self.__copy.__YLabel = self.__YLabel self.__copy.__YLabel = self.__YLabel
""" """
Send the original object when the window is closing Send the original object to the main window when the window is closing
""" """
def on_closing(self): def on_closing(self) -> None:
self.__copyWidget() self.__copyWidget()
self.__copy.__rebuild() self.__copy.__rebuild()
self.__root.destroy() self.__root.destroy()
......
...@@ -28,6 +28,7 @@ class PanelCommandes(LabelFrame): ...@@ -28,6 +28,7 @@ class PanelCommandes(LabelFrame):
frameBoutons.pack(side='bottom',fill='x') frameBoutons.pack(side='bottom',fill='x')
slider = Scale(frameBoutons,orient='horizontal',from_=1,to=10,command=self.notifyObserverAboutScale) slider = Scale(frameBoutons,orient='horizontal',from_=1,to=10,command=self.notifyObserverAboutScale)
slider.set(10)
slider.pack(side='bottom',fill='x') slider.pack(side='bottom',fill='x')
bouttonPlus = Button(frameBoutons,text='+',command=self.plus) bouttonPlus = Button(frameBoutons,text='+',command=self.plus)
......
"""
Class PanelPlotChart
"""
from tkinter import Frame, Toplevel
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class PanelPlotChart(Frame):
"""
Class PanelPlotChart
"""
def __init__(
self,
root : 'Tk',
id : int
) -> None :
super().__init__(root)
self.__root = root
self.__fig = Figure()
self.__axis = self.__fig.add_subplot()
self.__xAxis = [[]]
self.__yAxis = [[]]
self.__axis.plot(self.__xAxis,self.__yAxis)
self.__graphique = FigureCanvasTkAgg(self.__fig,master = self)
self.__graphique.get_tk_widget().pack(fill='both',expand='yes')
self.__title = ''
self.__XLabel = ''
self.__YLabel = ''
self.__policy = ['bo-']
self.__id = id
self.__copy = None
self.__observer = None
"""
Attach a observer to the object
"""
def attach(self, obs : 'Fenetre') -> None:
self.__observer = obs
"""
Notify the observer about an event
"""
def notify(self, panel : 'PanelBarChart', id : int) -> None:
self.__observer.updateGraphique(panel,id)
"""
Set the drawing policy for the curve identified by idCurve
"""
def setPolicy(self, idCurve : int, policy : str) -> None:
self.__axis.clear()
self.__policy[idCurve] = policy
self.__rebuild()
"""
Set the title to the chart
"""
def setTitle(self, name : str) -> None:
self.__axis.clear()
self.__title = name
self.__rebuild()
"""
Set the label on the x axis to the chart
"""
def setXLabel(self, name : str) -> None:
self.__axis.clear()
self.__XLabel = name
self.__rebuild()
"""
Set the label on the y axis to the chart
"""
def setYLabel(self, name : str) -> None:
self.__axis.clear()
self.__YLabel = name
self.__rebuild()
"""
Add a curve to the plot with the given policy
"""
def addCurve(self, policy : str) -> None:
self.__xAxis.append([])
self.__yAxis.append([])
self.__policy.append(policy)
"""
Add a point to the id curve with x,y coords
"""
def addPoint(self, id : int, x : int, y : int):
self.__axis.clear()
self.__xAxis[id].append(x)
self.__yAxis[id].append(y)
self.__rebuild()
"""
Rebuild the figure after any modification
"""
def __rebuild(self) -> None:
for i in range (len(self.__xAxis)):
self.__axis.plot(self.__xAxis[i],self.__yAxis[i],self.__policy[i])
self.__axis.title.set_text(self.__title)
self.__axis.set_xlabel(self.__XLabel)
self.__axis.set_ylabel(self.__YLabel)
self.__graphique.draw()
"""
Return a copy of the object
"""
def createCopy(self, name : str, id : int) -> 'PanelPlotChart':
window = Toplevel()
window.title(name)
self.__copy = PanelPlotChart(window, id)
self.__copy.pack(fill='both',expand='yes')
self.__copyWidget()
self.__copy.__rebuild()
self.__copy.__copy = self
self.__copy.__observer = self.__observer
window.protocol("WM_DELETE_WINDOW", self.__copy.on_closing)
window.geometry("+900+0")
return self.__copy
"""
Copy the object
"""
def __copyWidget(self) -> None:
self.__copy.__id = self.__id
self.__copy.__policy = self.__policy
self.__copy.__xAxis = self.__xAxis
self.__copy.__yAxis = self.__yAxis
self.__copy.__title = self.__title
self.__copy.__XLabel = self.__XLabel
self.__copy.__YLabel = self.__YLabel
"""
Send the original object to the main window when the window is closing
"""
def on_closing(self) -> None:
self.__copyWidget()
self.__copy.__rebuild()
self.__root.destroy()
self.notify(self.__copy, self.__copy.__id)
...@@ -92,6 +92,12 @@ class PanelVue(Canvas): ...@@ -92,6 +92,12 @@ class PanelVue(Canvas):
return self.create_image(x,y,image=new_image) return self.create_image(x,y,image=new_image)
"""
Draw a text with x,y coords
"""
def draw_text(self, x : float, y : float, text : str) -> int:
return self.create_text(x,y,anchor="w",fill='black',text=text)
""" """
Move an element to x,y coords Move an element to x,y coords
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment