Skip to content

Commit

Permalink
samples: update matplotlib sample using Wx (and remove deprecated tes…
Browse files Browse the repository at this point in the history
…t) (#1902)
  • Loading branch information
marcelotduarte authored Jun 5, 2023
1 parent 953042e commit f64265b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 67 deletions.
4 changes: 4 additions & 0 deletions samples/matplotlib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Here are samples to test cx_Freeze or to show how to use a package in cx_Freeze.

# Source

https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_wx2_sgskip.html

# Installation and requirements:

In a virtual environment, install by issuing the command:
Expand Down
23 changes: 0 additions & 23 deletions samples/matplotlib/matplotlib_afm.py

This file was deleted.

63 changes: 30 additions & 33 deletions samples/matplotlib/matplotlib_eg.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
"""A simple script to demonstrate matplotlib."""

from __future__ import annotations

import matplotlib
from numpy import arange, pi, sin

matplotlib.use("WXAgg")
import wx # noqa
from matplotlib.backends.backend_wx import NavigationToolbar2Wx # noqa
from matplotlib.backends.backend_wxagg import ( # noqa
from matplotlib.backends.backend_wxagg import (
FigureCanvasWxAgg as FigureCanvas,
NavigationToolbar2WxAgg as NavigationToolbar,
)
from matplotlib.figure import Figure # noqa
from matplotlib.figure import Figure

import numpy as np

import wx


class CanvasFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "CanvasFrame", size=(550, 350))
color = wx.Colour("WHITE")
self.SetBackgroundColour(color)
super().__init__(None, -1, "CanvasFrame", size=(550, 350))

self.figure = Figure()
self.axes = self.figure.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
self.axes = self.figure.add_subplot()
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2 * np.pi * t)

self.axes.plot(t, s)
self.canvas = FigureCanvas(self, -1, self.figure)

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizerAndFit(self.sizer)
self.add_toolbar()
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
self.SetSizer(self.sizer)
self.Fit()

self.add_toolbar() # comment this out for no toolbar

def add_toolbar(self):
self.toolbar = NavigationToolbar2Wx(self.canvas)
self.toolbar = NavigationToolbar(self.canvas)
self.toolbar.Realize()
if wx.Platform == "__WXMAC__":
self.SetToolBar(self.toolbar)
else:
tw, th = self.toolbar.GetSize()
fw, fh = self.canvas.GetSize()
self.toolbar.SetSize(wx.Size(fw, th))
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
# By adding toolbar in sizer, we are able to put it at the bottom
# of the frame - so appearance is closer to GTK version.
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
# update the axes menu on the toolbar
self.toolbar.update()

def OnPaint(self, event):
self.canvas.draw()


class App(wx.App):
def OnInit(self):
"""Create the main window and insert the custom frame"""
"""Create the main window and insert the custom frame."""
self.Init()
frame = CanvasFrame()
frame.Show(True)

return True


app = App(0)
app.MainLoop()
if __name__ == "__main__":
app = App()
app.MainLoop()
6 changes: 1 addition & 5 deletions samples/matplotlib/setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""A simple setup script to create two executables using matplotlib."""
"""A simple setup script to create executables using matplotlib."""

# matplotlib_eg.py is a very simple matplotlib application that demonstrates
# its use.
#
# matplotlib_afm.py is a very simple matplotlib console application that show
# some values of a font.
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
Expand Down Expand Up @@ -39,7 +36,6 @@
}

executables = [
Executable("matplotlib_afm.py"),
Executable("matplotlib_eg.py", base=base),
]

Expand Down
19 changes: 13 additions & 6 deletions samples/matplotlib/setup_zip.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
"""A simple setup script to create two executables using matplotlib
"""A simple setup script to create executables using matplotlib.
This version requires the mpl-data in the zip file."""

from __future__ import annotations

# matplotlib_eg.py is a very simple matplotlib application that demonstrates
# its use.
#
# matplotlib_afm.py is a very simple matplotlib console application that show
# some values of a font.
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
Expand All @@ -30,15 +27,25 @@
"build_exe": {
# Sometimes a little fine-tuning is needed
# exclude all backends except wx
"excludes": ["gtk", "PyQt4", "PyQt5", "tkinter"],
"excludes": [
"gi",
"gtk",
"PyQt4",
"PyQt5",
"PyQt6",
"PySide2",
"PySide6",
"shiboken2",
"shiboken6",
"tkinter",
],
"zip_include_packages": ["*"],
"zip_exclude_packages": [],
"build_exe": build_exe_dir,
}
}

executables = [
Executable("matplotlib_afm.py"),
Executable("matplotlib_eg.py", base=base),
]

Expand Down

0 comments on commit f64265b

Please sign in to comment.