diff --git a/samples/matplotlib/README.md b/samples/matplotlib/README.md index 680d4ff74..2867da14b 100644 --- a/samples/matplotlib/README.md +++ b/samples/matplotlib/README.md @@ -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: diff --git a/samples/matplotlib/matplotlib_afm.py b/samples/matplotlib/matplotlib_afm.py deleted file mode 100644 index af56b0e1a..000000000 --- a/samples/matplotlib/matplotlib_afm.py +++ /dev/null @@ -1,23 +0,0 @@ -"""A simple script to show some values of a font in matplotlib.""" - -from __future__ import annotations - -from pathlib import Path - -import matplotlib as mpl -from matplotlib.afm import AFM - -print("matplotlib datapath:", mpl.get_data_path()) -afm_path = Path(mpl.get_data_path(), "fonts", "afm", "ptmr8a.afm") -with afm_path.open("rb") as fh: - afm = AFM(fh) -print( - "compare the results with:", - "https://github.com/matplotlib/matplotlib/" - "blob/master/lib/matplotlib/afm.py", -) -print(afm.string_width_height("What the heck?")) -print(afm.get_fontname()) -print(afm.get_kern_dist("A", "f")) -print(afm.get_kern_dist("A", "y")) -print(afm.get_bbox_char("!")) diff --git a/samples/matplotlib/matplotlib_eg.py b/samples/matplotlib/matplotlib_eg.py index b91a80297..c36829c2c 100644 --- a/samples/matplotlib/matplotlib_eg.py +++ b/samples/matplotlib/matplotlib_eg.py @@ -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() diff --git a/samples/matplotlib/setup.py b/samples/matplotlib/setup.py index 80dc1ebe0..ad022ff44 100644 --- a/samples/matplotlib/setup.py +++ b/samples/matplotlib/setup.py @@ -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 @@ -39,7 +36,6 @@ } executables = [ - Executable("matplotlib_afm.py"), Executable("matplotlib_eg.py", base=base), ] diff --git a/samples/matplotlib/setup_zip.py b/samples/matplotlib/setup_zip.py index 0ba8ebc80..6674f77f1 100644 --- a/samples/matplotlib/setup_zip.py +++ b/samples/matplotlib/setup_zip.py @@ -1,4 +1,4 @@ -"""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 @@ -6,9 +6,6 @@ # 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 @@ -30,7 +27,18 @@ "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, @@ -38,7 +46,6 @@ } executables = [ - Executable("matplotlib_afm.py"), Executable("matplotlib_eg.py", base=base), ]