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

1

I know that tkinter is not thread-safe , but I honestly do not know yet what that means. We are creating a game with pygame , and I would like to integrate an start menu and console written with tkinter, but I do not know if it is possible, and if it is a good idea. If it is possible, how can I do it, and what are the implications and risks I may have?

    
asked by anonymous 04.12.2014 / 12:59

2 answers

2

It is not possible to combine the two libraries because they are graphical libraries with different goals, not to say competing with each other. It is rather possible to write a program that creates both a pygame window and another tkinter at the same time, but not the tkinter inside the pygame, in full screen, for example.

It would be an error if we tried 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 video card of the machine, on the other hand 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. Soon, pygame is not able to have a default button like tkinter, because the click would not work. At this point pygame is more primitive: it asks the operating system to create a window, and within it, it takes full responsibility.

In contrast, in tkinter, all events that the operating system has are already listened to by the application, including the main loop, in the mainloop() function. The application just does not react because there is no callback function, made by the programmer, with instructions for each of these events. That's why tkinter can afford to have elaborate controls like buttons, comboboxes, and others, but always undergo all the standard visual definitions of the system, such as screen resolution, which pygame does not have to do. >

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

    
15.01.2015 / 23:59
2

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 to a string containing id of the 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's 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 Pygame Documentation

    
20.01.2015 / 08:50