Skip to content

Commit d112f9c

Browse files
committed
adding gui window for the plot
1 parent eb1040c commit d112f9c

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

pygame_matplotlib/gui_window.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Contain a window with a plot for pygame_gui."""
2+
from typing import Union
3+
4+
import pygame
5+
import pygame_gui
6+
from pygame_gui.core.interfaces.manager_interface import IUIManagerInterface
7+
from pygame_gui.core.ui_element import ObjectID
8+
9+
from .backend_pygame import FigureSurface
10+
import matplotlib
11+
matplotlib.use('module://pygame_matplotlib.backend_pygame')
12+
13+
class PlotWindow(pygame_gui.elements.ui_window.UIWindow):
14+
def __init__(
15+
self,
16+
rect: pygame.Rect,
17+
manager: IUIManagerInterface,
18+
figuresurface: FigureSurface,
19+
window_display_title: str = "",
20+
element_id: Union[str, None] = None,
21+
object_id: Union[ObjectID, str, None] = None,
22+
resizable: bool = False,
23+
visible: int = 1
24+
):
25+
self.figuresurf = figuresurface
26+
super().__init__(
27+
rect, manager,
28+
window_display_title=window_display_title,
29+
element_id=element_id,
30+
object_id=object_id,
31+
resizable=resizable,
32+
visible=visible
33+
)
34+
35+
def set_dimensions(self, *args, **kwargs):
36+
super().set_dimensions(*args, **kwargs)
37+
# Update the size of the figure with the new bounding rectangle
38+
self.figuresurf.set_bounding_rect(
39+
self.get_container().get_rect()
40+
)
41+
# Update the image of the container
42+
self.get_container().set_image(self.figuresurf)

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pygame-matplotlib
3-
version = 0.2
3+
version = 0.2.1
44
author = Lionel42
55
description = A matplotlib backend using pygame.
66
long_description = file: README.md
@@ -23,6 +23,7 @@ packages = find:
2323
install_requires =
2424
matplotlib
2525
pygame >= 2.0.0
26+
pygame_gui
2627
python_requires = >=3.9
2728

2829
[options.packages.find]

test_gui_window.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""Simple script to test from https://pygame-gui.readthedocs.io/en/latest/quick_start.html"""
2+
3+
from typing import Tuple, Union
4+
import pygame
5+
import pygame_gui
6+
from pygame_gui.core.interfaces.manager_interface import IUIManagerInterface
7+
from pygame_gui.core.ui_element import ObjectID
8+
9+
from pygame_gui.elements.ui_window import UIWindow
10+
from pygame_gui.core.utility import basic_blit
11+
12+
13+
import matplotlib.pyplot as plt
14+
15+
from pygame_matplotlib.gui_window import PlotWindow
16+
17+
18+
pygame.init()
19+
20+
pygame.display.set_caption('Test')
21+
window_surface = pygame.display.set_mode((800, 600))
22+
23+
background = pygame.Surface((800, 600))
24+
background.fill(pygame.Color('#000000'))
25+
26+
manager = pygame_gui.UIManager((800, 600))
27+
28+
fig, axes = plt.subplots(1, 1,)
29+
axes.plot([1,2], [1,2], color='green', label='test')
30+
fig.canvas.draw()
31+
32+
33+
fig2, axes2 = plt.subplots(1, 1,)
34+
axes2.plot([1,2], [1,2], color='blue', label='test')
35+
fig2.canvas.draw()
36+
37+
38+
plot_window = PlotWindow(
39+
rect=pygame.Rect((350, 275), (300, 200)),
40+
manager=manager,
41+
figuresurface=fig,
42+
resizable=True
43+
)
44+
45+
46+
47+
48+
plot_window2 = PlotWindow(
49+
rect=pygame.Rect((350, 275), (300, 200)),
50+
manager=manager,
51+
figuresurface=fig2,
52+
resizable=True
53+
)
54+
55+
56+
clock = pygame.time.Clock()
57+
58+
is_running = True
59+
60+
while is_running:
61+
time_delta = clock.tick(60)/1000.0
62+
for event in pygame.event.get():
63+
if event.type == pygame.QUIT:
64+
is_running = False
65+
66+
67+
manager.process_events(event)
68+
69+
manager.update(time_delta)
70+
71+
# Blitts and draw
72+
window_surface.blit(background, (0, 0))
73+
manager.draw_ui(window_surface)
74+
75+
76+
# print(plot_window2.get_container().get_size())
77+
78+
pygame.display.update()

0 commit comments

Comments
 (0)