Is it possible to create a communication between a Tkinter application and a pygame?

Asked

Viewed 584 times

1

I know that tkinter is not thread-safe, but honestly I’m not quite sure what that means yet. We’re creating a game with pygame, and would like to integrate a menu initial and a console written with Tkinter, but I don’t know if it’s possible, and if it’s a good idea. If it is possible, how can I do it, and what are the implications and risks I may have?

2 answers

1

Yes, it is possible. At least it is possible to embed pygame in Tkinter.

On some platforms it is possible to embed the pygame display in an existing window. To do this, the environment variable SDL_WINDOWID must be set with a string containing the id window.

The environment variable is checked when the pygame display is initialized. But stay tuned, as there may be some strange effects when running the pygame embedded in another display.

Here an example:

import platform
import os
import pygame

#Assumindo que embed é um tk.Frame previamente criado
#Seta a id da janela (Windows ID)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
#Seta o driver de vídeo caso for Windows
if platform.system == "Windows":
    os.environ['SDL_VIDEODRIVER'] = 'windib'
screen = pygame.display.set_mode((500,500))
screen.fill(pygame.Color(255,255,255))
pygame.display.init()
pygame.display.flip()

Sources: Stack user Pythonnut and Documentation of the Pygame

1

Is not possible combine the two libraries because they are graphic libraries with different goals, not to say competitors among themselves. It is possible to write a program that creates both a pygame and a Tkinter window at the same time, but not the Tkinter inside the pygame, in full screen, for example.

An error would be generated if we try to say that the master of a Tkinter frame is the pygame window, because the expected type is different.

If on the one hand pygame has many properties and functions that work with the machine’s graphics card, on the other it requires that until the main loop of the application is implemented. Even a simple mouse click needs to have your event listened to so that, in fact, the click exists in the application. Therefore, pygame is not able to have a default button as in Tkinter, because the click would not work. At this point the pygame is more primitive: it asks the operating system to create a window, and within it, it takes full responsibility.

On the other hand, in Tkinter, all the events that the operating system has are already heard by the application, including the main loop exists, in the function mainloop(). The application does not react because there is no callback function, made by the programmer, with the instructions for each of these events. That’s why Tkinter can afford to have elaborate controls like buttons, comboboxes, and others, but always submitting to all the system’s standard visual settings, like screen resolution, which pygame doesn’t have to do.

It is possible to implement in Tkinter something small, 2D and even better if it is static, like a chess game for example. You can use canvas. In this case I recommend studying the technique of double buffer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.