2

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.

Chris Ze Third
  • 632
  • 2
  • 18
  • Please refer to this guide on how to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), and read about [how to ask](https://stackoverflow.com/help/how-to-ask). Remember, we can't help you if we don't know what you've already tried. – JRiggles Oct 26 '22 at 14:27
  • @JRiggles I tried to provide extra information! – Chris Ze Third Oct 26 '22 at 14:49
  • too much text to read, so please reduce. Also, I notice that the class `def __init__()` is not formed correctly as the first argument should be `self`... – D.L Oct 26 '22 at 15:31
  • 1
    I have actually published a book that contains conways game of life (using `matplotlib` in `python`). You might wish to take a look. (i did not use tkinter tho) and show and alternative in `pygame`: https://www.amazon.co.uk/dp/B0BHL2XKCR – D.L Oct 26 '22 at 15:34
  • 1
    @D.L well that's awesome. I was also considering using `pygame` so I will check that thanks! – Chris Ze Third Oct 26 '22 at 15:36
  • @D.L The `self` was just a typo, the code has this argument – Chris Ze Third Oct 26 '22 at 15:39
  • Are you using matplotlib animation or just matplot lib charts ? – D.L Oct 26 '22 at 15:42
  • I am using matplotlib animation, I don't know what charts are supposed to be – Chris Ze Third Oct 26 '22 at 15:45

0 Answers0