2019-04-28 18:47:26 +00:00
|
|
|
import pygame
|
|
|
|
import PySimpleGUI as sg
|
|
|
|
import os
|
|
|
|
|
|
|
|
"""
|
|
|
|
Demo of integrating PyGame with PySimpleGUI, the tkinter version
|
|
|
|
A similar technique may be possible with WxPython
|
2019-10-23 20:10:03 +00:00
|
|
|
To make it work on Linux, set SDL_VIDEODRIVER like
|
|
|
|
specified in http://www.pygame.org/docs/ref/display.html, in the
|
|
|
|
pygame.display.init() section.
|
2019-04-28 18:47:26 +00:00
|
|
|
"""
|
|
|
|
# --------------------- PySimpleGUI window layout and creation --------------------
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Text('Test of PySimpleGUI with PyGame')],
|
|
|
|
[sg.Graph((500, 500), (0, 0), (500, 500),
|
|
|
|
background_color='lightblue', key='-GRAPH-')],
|
|
|
|
[sg.Button('Draw'), sg.Exit()]]
|
2019-04-28 18:47:26 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('PySimpleGUI + PyGame', layout, finalize=True)
|
|
|
|
graph = window['-GRAPH-']
|
2019-04-28 18:47:26 +00:00
|
|
|
|
|
|
|
# -------------- Magic code to integrate PyGame with tkinter -------
|
|
|
|
embed = graph.TKCanvas
|
|
|
|
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
|
2019-12-24 23:52:47 +00:00
|
|
|
# change this to 'x11' to make it work on Linux
|
|
|
|
os.environ['SDL_VIDEODRIVER'] = 'windib'
|
2019-04-28 18:47:26 +00:00
|
|
|
|
|
|
|
# ----------------------------- PyGame Code -----------------------------
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
screen = pygame.display.set_mode((500, 500))
|
|
|
|
screen.fill(pygame.Color(255, 255, 255))
|
2019-04-28 18:47:26 +00:00
|
|
|
|
|
|
|
pygame.display.init()
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read(timeout=10)
|
2020-05-07 10:22:59 +00:00
|
|
|
if event in (sg.WIN_CLOSED, 'Exit'):
|
2019-04-28 18:47:26 +00:00
|
|
|
break
|
|
|
|
elif event == 'Draw':
|
|
|
|
pygame.draw.circle(screen, (0, 0, 0), (250, 250), 125)
|
|
|
|
pygame.display.update()
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|