Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put a pygame in a tkinter application?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading