-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
WxAgg NavigationToolbar2 coordinate display not working as expected #18561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Not an expert in wx, but this rather seems like an ill-defined layout. AddStretchableSpace() is only relevant in the sense that it moves content to the right side of the toolbar. The underlying problem is that the new controls are drawn on top of the toolbar. I doubt that there is anything we can do from the matplotlib side. |
|
Ah, OK, that makes a bit more sense now. It still has issues with drawing the coordinates on top of the other controls, but that's an easy fix by removing the FWIW, the more general approach to add extra UI elements of any type is to use I think that we should maybe add an option to place the coordinates elsewhere on the toolbar, but I'm happy to close this issue now. Thanks for all the help and advice Full working code, just in case its useful for anyone: import wx
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg, FigureCanvasWxAgg
from matplotlib.backends.backend_wx import NavigationToolbar2
class NewNavigationToolbar2WxAgg(NavigationToolbar2WxAgg):
def __init__(self, canvas, coordinates=True, spacer='stretch'):
"""
Parameters
----------
canvas : FigureCanvasWxAgg
Instance of canvas that the toolbar connects to.
coordinates : bool
Enable coordinate display when mouse is hovered over an axes instance in the figure.
Defaults to True.
spacer : str
Option to change the default behaviour of stretching the coordinate display to the far
right of the toolbar. Only relevant when coordinates = True.
Valid values are 'stretch' (place coordinate in right corner) or 'sep' (add a separator then place
coordinate text next to the toolbar buttons).
Defaults to 'stretch'
"""
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
if 'wxMac' in wx.PlatformInfo:
self.SetToolBitmapSize((24, 24))
self.wx_ids = {}
for text, tooltip_text, image_file, callback in self.toolitems:
if text is None:
self.AddSeparator()
continue
self.wx_ids[text] = (
self.AddTool(
-1,
bitmap=self._icon(f"{image_file}.png"),
bmpDisabled=wx.NullBitmap,
label=text, shortHelp=tooltip_text,
kind=(wx.ITEM_CHECK if text in ["Pan", "Zoom"]
else wx.ITEM_NORMAL))
.Id)
self.Bind(wx.EVT_TOOL, getattr(self, callback),
id=self.wx_ids[text])
self._coordinates = coordinates
if self._coordinates:
if spacer == 'stretch':
self.AddStretchableSpace()
elif spacer == 'sep':
self.AddSeparator()
else:
raise ValueError(f"Invalid selection for spacer keyword. Should be 'stretch' or 'sep'")
self._label_text = wx.StaticText(self) #, style=wx.ALIGN_CENTER, size=(coord_width, -1))
self.AddControl(self._label_text)
self.Realize()
NavigationToolbar2.__init__(self, canvas)
self._idle = True
self._prevZoomRect = None
# for now, use alternate zoom-rectangle drawing on all
# Macs. N.B. In future versions of wx it may be possible to
# detect Retina displays with window.GetContentScaleFactor()
# and/or dc.GetContentScaleFactor()
self._retinaFix = 'wxMac' in wx.PlatformInfo
class PlotFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
wx.Frame.__init__(self, parent, *args, **kwargs)
self.fig = plt.figure(figsize=(5, 4), dpi=60)
self.subplot1 = self.fig.add_subplot(111)
self.canvas = FigureCanvasWxAgg(self, wx.ID_ANY, self.fig)
self.toolbar = NewNavigationToolbar2WxAgg(self.canvas, spacer='sep')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.EXPAND)
# Add more wx controls to the same area as the toolbar
export_data_btn = wx.Button(self.toolbar, label="Export Data")
some_other_control = wx.TextCtrl(self.toolbar, value="TextCtrl")
sizer.Add(self.toolbar, 0, wx.EXPAND)
self.toolbar.AddStretchableSpace() # right align the new controls
self.toolbar.AddControl(export_data_btn)
self.toolbar.AddControl(some_other_control, label="Enter text: ")
self.toolbar.Realize()
self.SetSizer(sizer)
self.Layout()
self.CenterOnScreen()
if __name__ == '__main__':
app = wx.App(False)
frame = PlotFrame(None, title='Test Frame', size=(800, 600))
app.SetTopWindow(frame)
frame.Center()
frame.Show()
app.MainLoop() which produces: |
Bug report
Bug summary
Related to issue #18212, the coordinate display added in 3.3 is not working as expected when the toolbar is placed inside a horizontal BoxSizer, such as when adding extra wx controls to the 'empty' space to the right of the toolbar.
Code for reproduction
Actual outcome
The coordinate display clips off the side of the toolbar, as shown
Suggested fix
Adding a keyword argument to NavigationToolbar2WxAgg to remove the AddStretchableSpace() from the toolbar init method is one work-around, but the coordinates then appear next to the toolbar buttons. I haven't found a way to keep the coordinates right-aligned and displayed correctly.
Matplotlib version
print(matplotlib.get_backend())
): WxAggThe text was updated successfully, but these errors were encountered: