I'm struggling with this issue for some time so I have decided to finally ask here.
First of the code is available at this adress: https://github.com/ChrisZeThird/Game-Of-Life/tree/main/ObjectOriented (it would be too long to copy and paste it).
I have successfully created the Conway's Game of Life, and created an alternative version so 2 persons can play the game. My goal right now is to create an exe
file so anyone can play it without having to download libraries or python.
But I just don't understand how to insert my matploltib
figure inside a tkinter
window. I've searched online, found several answers from different posts, but I don't see how to apply those solution to my problem, especially because I created classes. Can I create the root window outside the class or should I create it in the init
of my classes. I kinda see how to add the buttons, and connect them to my different functions, but what about the animation? Creating buttons resulted in an embedded plot in tkinter, an empty matplotlib window being displayed and a non working animation (only the plot interactivity when you click on a cell works).
I'm just very frustrated by this issue, and several unsuccessful days of research. So I'd be grateful if anyone had at least a small indication or a vague explanation.
I've tried adding the root window to the class, using canvas and FigureCanvasTkAgg
. This places the figure inside the tkinter window but it isn't animated anymore.
Class Versus():
def __init__(self,N):
...
self.root = init_figure.root
self.root.title("Conway's Game of Life - by ChrisZeThird" )
self.root.geometry("1080x720")
self.fig = init_figure.fig
self.ax = init_figure.ax
self.fig.suptitle(f'Each player has {self.nbr} cells to place', fontsize=20)
self.canvas = FigureCanvasTkAgg(self.fig, self.root)
...
## I translated matplotlib buttons to tkinter buttons
...
self.canvas.get_tk_widget().pack(fill="both", expand=True)
and later on I have self.canvas.mpl_connect('button_press_event', self.turn_on) # connect cells management to figure
. I also removed the event
variable in the button commands and remove the button axes and definition to replace them with tkinter button.
I tried doing it externally to the class. So I made another file and called the class there.
versus = cv.Versus(N)
fig = versus.fig
# Creating Canvas
canv = FigureCanvasTkAgg(fig, master = root)
canv.draw()
get_widz = canv.get_tk_widget()
get_widz.pack()
And that generated a separated figure, so not embedded.