PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it


PyGame in combination with other libraries

Tkinter

Related Stack Overflow questions:

The following examples show how to embed a Pygame window in a Tkinter frame. On Windows, this example works only with Pygame 2.2.0 or later:

📁 Minimal example - tkinter

import tkinter as tk
import pygame
import os, random

root = tk.Tk()
button_win = tk.Frame(root, width = 500, height = 25)
button_win.pack(side = tk.TOP)
embed_pygame = tk.Frame(root, width = 500, height = 500)
embed_pygame.pack(side = tk.TOP)

os.environ['SDL_WINDOWID'] = str(embed_pygame.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
pygame.display.init()
screen = pygame.display.set_mode()

def random_color():
    global circle_color
    circle_color = pygame.Color(0)
    circle_color.hsla = (random.randrange(360), 100, 50, 100)

random_color() 
color_button = tk.Button(button_win, text = 'random color',  command = random_color)
color_button.pack(side=tk.LEFT)

def pygame_loop():
    screen.fill((255, 255, 255))
    pygame.draw.circle(screen, circle_color, (250, 250), 125)
    pygame.display.flip()
    root.update()  
    root.after(100, pygame_loop)

pygame_loop()
tk.mainloop()

PySimpleGUI

PySimpleGUI

Related Stack Overflow questions:

PygameMenu

pygame-menu

MathPlotLib

mathplotlib

Related Stack Overflow questions:

PyQt

Related Stack Overflow questions:

Do not mix frameworks. The frameworks may interact poorly with each other or conflict completely. If it works on your (operating) system, that doesn’t mean it will work on another (operating) system or with a different version of one of the frameworks. Mixing frameworks always means some kind of undefined behavior.

In your example your create an image (pygae.Surface) with the Pygame library and display it in QWidget. You never create a Pygame window. Therefore the Pygame event handling cannot work. You need to use Qts event handling.

Anyway, if you just want to do some image processing or draw some pictures and display them in a Qt application, I suggest using OpenCV (cv2). This library is designed for powerful image manipulation and the images can be viewed nicely using a Qt user interface.