Skip to content

Commit 2ded37a

Browse files
committed
Merge pull request #897 from pelson/gui_icon
GUI icon in Tkinter
2 parents bd5ca33 + 1706dd3 commit 2ded37a

File tree

5 files changed

+65
-47
lines changed

5 files changed

+65
-47
lines changed

lib/matplotlib/backends/backend_gtk3.py

+27-28
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,17 @@ def __init__(self, canvas, num):
359359

360360
self.window = Gtk.Window()
361361
self.set_window_title("Figure %d" % num)
362-
if (window_icon):
363-
try:
364-
self.window.set_icon_from_file(window_icon)
365-
except:
366-
# some versions of gtk throw a glib.GError but not
367-
# all, so I am not sure how to catch it. I am unhappy
368-
# diong a blanket catch here, but an not sure what a
369-
# better way is - JDH
370-
verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])
362+
try:
363+
self.window.set_icon_from_file(window_icon)
364+
except (SystemExit, KeyboardInterrupt):
365+
# re-raise exit type Exceptions
366+
raise
367+
except:
368+
# some versions of gtk throw a glib.GError but not
369+
# all, so I am not sure how to catch it. I am unhappy
370+
# doing a blanket catch here, but am not sure what a
371+
# better way is - JDH
372+
verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])
371373

372374
self.vbox = Gtk.Box()
373375
self.vbox.set_property("orientation", Gtk.Orientation.VERTICAL)
@@ -562,12 +564,15 @@ def configure_subplots(self, button):
562564

563565

564566
window = Gtk.Window()
565-
if (window_icon):
566-
try: window.set_icon_from_file(window_icon)
567-
except:
568-
# we presumably already logged a message on the
569-
# failure of the main plot, don't keep reporting
570-
pass
567+
try:
568+
window.set_icon_from_file(window_icon)
569+
except (SystemExit, KeyboardInterrupt):
570+
# re-raise exit type Exceptions
571+
raise
572+
except:
573+
# we presumably already logged a message on the
574+
# failure of the main plot, don't keep reporting
575+
pass
571576
window.set_title("Subplot Configuration Tool")
572577
window.set_default_size(w, h)
573578
vbox = Gtk.Box()
@@ -963,7 +968,6 @@ def get_active_line(self):
963968
line = self.lines[ind]
964969
return line
965970

966-
967971
def get_active_linestyle(self):
968972
'get the active lineinestyle'
969973
ind = self.cbox_linestyles.get_active()
@@ -997,8 +1001,6 @@ def _update(self):
9971001

9981002
line.figure.canvas.draw()
9991003

1000-
1001-
10021004
def on_combobox_lineprops_changed(self, item):
10031005
'update the widgets from the active line'
10041006
if not self._inited: return
@@ -1044,17 +1046,14 @@ def on_dialog_lineprops_okbutton_clicked(self, button):
10441046
def on_dialog_lineprops_cancelbutton_clicked(self, button):
10451047
self.dlg.hide()
10461048

1047-
# set icon used when windows are minimized
1048-
try:
10491049

1050-
if sys.platform == 'win32':
1051-
icon_filename = 'matplotlib.png'
1052-
else:
1053-
icon_filename = 'matplotlib.svg'
1054-
window_icon = os.path.join(rcParams['datapath'], 'images', icon_filename)
1055-
except:
1056-
window_icon = None
1057-
verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])
1050+
# Define the file to use as the GTk icon
1051+
if sys.platform == 'win32':
1052+
icon_filename = 'matplotlib.png'
1053+
else:
1054+
icon_filename = 'matplotlib.svg'
1055+
window_icon = os.path.join(matplotlib.rcParams['datapath'], 'images', icon_filename)
1056+
10581057

10591058
def error_msg_gtk(msg, parent=None):
10601059
if parent is not None: # find the toplevel Gtk.Window

lib/matplotlib/backends/backend_tkagg.py

+17
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ def new_figure_manager(num, *args, **kwargs):
7878
FigureClass = kwargs.pop('FigureClass', Figure)
7979
figure = FigureClass(*args, **kwargs)
8080
window = Tk.Tk()
81+
82+
if Tk.TkVersion >= 8.5:
83+
# put a mpl icon on the window rather than the default tk icon. Tkinter
84+
# doesn't allow colour icons on linux systems, but tk >=8.5 has a iconphoto
85+
# command which we call directly. Source:
86+
# http://mail.python.org/pipermail/tkinter-discuss/2006-November/000954.html
87+
icon_fname = os.path.join(rcParams['datapath'], 'images', 'matplotlib.gif')
88+
icon_img = Tk.PhotoImage(file=icon_fname)
89+
try:
90+
window.tk.call('wm', 'iconphoto', window._w, icon_img)
91+
except (SystemExit, KeyboardInterrupt):
92+
# re-raise exit type Exceptions
93+
raise
94+
except:
95+
# log the failure, but carry on
96+
verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])
97+
8198
canvas = FigureCanvasTkAgg(figure, master=window)
8299
figManager = FigureManagerTkAgg(canvas, num, window)
83100
if matplotlib.is_interactive():

lib/matplotlib/backends/backend_wx.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from __future__ import division, print_function
22
"""
3-
4-
backend_wx.py
5-
63
A wxPython backend for matplotlib, based (very heavily) on
74
backend_template.py and backend_gtk.py
85
@@ -18,14 +15,17 @@
1815
1916
"""
2017

21-
cvs_id = '$Id$'
18+
import sys
19+
import os
20+
import os.path
21+
import math
22+
import StringIO
23+
import weakref
24+
import warnings
2225

23-
24-
import sys, os, os.path, math, StringIO, weakref, warnings
2526
import numpy as np
2627

2728

28-
2929
# Debugging settings here...
3030
# Debug level set here. If the debug level is less than 5, information
3131
# messages (progressively more info for lower value) are printed. In addition,
@@ -188,7 +188,6 @@ def __init__(self, parent, *args, **kwargs):
188188

189189
# Unbinding causes Wx to stop for some reason. Disabling for now.
190190
# def __del__(self):
191-
# import wx
192191
# TimerBase.__del__(self)
193192
# self.parent.Bind(wx.EVT_TIMER, None, self._timer)
194193

@@ -410,7 +409,6 @@ def get_gc(self):
410409
assert self.gc != None, "gc must be defined"
411410
return self.gc
412411

413-
414412
def get_wx_font(self, s, prop):
415413
"""
416414
Return a wx font. Cache instances in a font dictionary for
@@ -448,14 +446,14 @@ def get_wx_font(self, s, prop):
448446

449447
return font
450448

451-
452449
def points_to_pixels(self, points):
453450
"""
454451
convert point measures to pixes using dpi and the pixels per
455452
inch of the display
456453
"""
457454
return points*(PIXELS_PER_INCH/72.0*self.dpi/72.0)
458455

456+
459457
class GraphicsContextWx(GraphicsContextBase):
460458
"""
461459
The graphics context provides the color, line styles, etc...
@@ -627,6 +625,7 @@ def get_wxcolour(self, color):
627625
a *= 255
628626
return wx.Colour(red=int(r), green=int(g), blue=int(b), alpha=int(a))
629627

628+
630629
class FigureCanvasWx(FigureCanvasBase, wx.Panel):
631630
"""
632631
The FigureCanvas contains the figure and does event handling.
@@ -1421,7 +1420,7 @@ def _create_wx_app():
14211420
# retain a reference to the app object so it does not get garbage
14221421
# collected and cause segmentation faults
14231422
_create_wx_app.theWxApp = wxapp
1424-
1423+
14251424

14261425
def draw_if_interactive():
14271426
"""
@@ -1512,6 +1511,15 @@ def __init__(self, num, fig):
15121511
self.Fit()
15131512

15141513
self.canvas.SetMinSize((2, 2))
1514+
1515+
# give the window a matplotlib icon rather than the stock one.
1516+
# This is not currently working on Linux and is untested elsewhere.
1517+
#icon_path = os.path.join(matplotlib.rcParams['datapath'],
1518+
# 'images', 'matplotlib.png')
1519+
#icon = wx.IconFromBitmap(wx.Bitmap(icon_path))
1520+
# for xpm type icons try:
1521+
#icon = wx.Icon(icon_path, wx.BITMAP_TYPE_XPM)
1522+
#self.SetIcon(icon)
15151523

15161524
self.figmgr = FigureManagerWx(self.canvas, num, self)
15171525

@@ -1559,15 +1567,14 @@ def Destroy(self, *args, **kwargs):
15591567
wxapp.Yield()
15601568
return True
15611569

1570+
15621571
class FigureManagerWx(FigureManagerBase):
15631572
"""
15641573
This class contains the FigureCanvas and GUI frame
15651574
15661575
It is instantiated by GcfWx whenever a new figure is created. GcfWx is
15671576
responsible for managing multiple instances of FigureManagerWx.
15681577
1569-
NB: FigureManagerBase is found in _pylab_helpers
1570-
15711578
public attrs
15721579
15731580
canvas - a FigureCanvasWx(wx.Panel) instance
@@ -1599,7 +1606,6 @@ def destroy(self, *args):
15991606
DEBUG_MSG("destroy()", 1, self)
16001607
self.frame.Destroy()
16011608
#if self.tb is not None: self.tb.Destroy()
1602-
import wx
16031609
#wx.GetApp().ProcessIdle()
16041610
wx.WakeUpIdle()
16051611

@@ -1939,11 +1945,6 @@ def set_history_buttons(self):
19391945

19401946
class NavigationToolbarWx(wx.ToolBar):
19411947
def __init__(self, canvas, can_kill=False):
1942-
"""
1943-
figure is the Figure instance that the toolboar controls
1944-
1945-
win, if not None, is the wxWindow the Figure is embedded in
1946-
"""
19471948
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
19481949
DEBUG_MSG("__init__()", 1, self)
19491950
self.canvas = canvas
1.47 KB
Loading

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
'mpl-data/fonts/ttf/RELEASENOTES.TXT',
9999
'mpl-data/images/*.xpm',
100100
'mpl-data/images/*.svg',
101+
'mpl-data/images/*.gif',
101102
'mpl-data/images/*.png',
102103
'mpl-data/images/*.ppm',
103104
'mpl-data/example/*.npy',

0 commit comments

Comments
 (0)