Home > other >  How to put a pygame in a tkinter application?
How to put a pygame in a tkinter application?

Time:01-10

I programmed a board game in pygame, which is cool and all but I'd like to put it in a tkinter window so that I have space above my game, in which I can put buttons for resetting the board, etc.

How do I embed this? I would like to only have a tkinter window and not a seperate pygame window.

I made a window using

win = pygame.display.set_mode((WIDTH, HEIGHT)
pygame.display.set_caption('Game')

And then I displayed it using a while loop with

pygame.display.update()

I genuinely don't know how to make a tkinter that embeds this.

I tried using this solution, however when I ran this program I got two windows, which isn't what I want

CodePudding user response:

This is complicated. Both toolkits have their own windowing subsystems.

I remember once coming across a way to have Pygame's SDL render into a canvas in another window - but I can't now recall if that was X11 (~Linux) specific.

You will jabe to either have your buttons and controls rendered by Pygame - in that case there are complementary libraries such as Phil's Pygame Utilities ("PGU" - I have republished a version of it for Python3 https://pypi.org/project/pygame-pgu/ ) or another similar library.

Another option is to have your game application use 2 independent, related windows - in this case, running tkinter in a separate thread should be enough to have both working - or calling the .update() method on the root tkinter object in the pygame mainloop should suffice.

  • Related