0

I have made a simulation in pygame. It makes use of a controlled value that the user can set through a tkinter slider and also has a graph displaying some of its statistics (made with matplotlib). Since the pygame, matplotlib and tkinter were all separate windows and I wanted them all in one place as in an app, I first embedded the matplotlib graph into the tk box using FigureCanvasTkAgg. I then tried the same with the pygame window. Having not found an answer on the internet, I then cobbled together a solution:

class PygameWin():
    def __init__(self, root):
        self.surface = pygame.display.set_mode(
            [globalvars.screen_width, globalvars.screen_height])
        self.surface.fill((255, 255, 255))
        self.canvas = tk.Canvas(
            root, height=globalvars.screen_height, width=globalvars.screen_width)

    
    def update(self):
        globalvars.amoebas.update()
        
        self.surface.fill((255, 255, 255))
        globalvars.amoebas.draw(self.surface)
        pygame.image.save(self.surface, 'pygamewin.png')
        self.img = ImageTk.PhotoImage(Image.open("pygamewin.png"))
        self.canvas.create_image(250,275, image=self.img)
        self.canvas.pack()

It basically saves the pygame window as an image and then adds it into a canvas in the tk interface. It works as intended except for the fact that it is very slow and only updates every 100 ticks (times the amoeba are updated). Could anyone help me make it more efficient? I realise that other parts of my code will be impacting the speed, and I can add the full program in if needed.

P.S. Although I can see the pygame window in the tk box as planned, it is still showing up as a separate window as well, so if anyone could tell me how to get rid of that it would be much appreciated.

AtomProgrammer
  • 245
  • 3
  • 13

0 Answers0