Skip to content

GUI icon in Tkinter #897

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

Merged
merged 6 commits into from
Aug 1, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Checked gtk backend & simplified some icon related code.
  • Loading branch information
Phil Elson authored and pelson committed Jul 29, 2012
commit 25df1bb57dfdd305d8988ed8d29ace04b481c55d
55 changes: 27 additions & 28 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,17 @@ def __init__(self, canvas, num):

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"an" --> "am"

# better way is - JDH
verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])

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


window = Gtk.Window()
if (window_icon):
try: window.set_icon_from_file(window_icon)
except:
# we presumably already logged a message on the
# failure of the main plot, don't keep reporting
pass
try:
window.set_icon_from_file(window_icon)
except (SystemExit, KeyboardInterrupt):
# re-raise exit type Exceptions
raise
except:
# we presumably already logged a message on the
# failure of the main plot, don't keep reporting
pass
window.set_title("Subplot Configuration Tool")
window.set_default_size(w, h)
vbox = Gtk.Box()
Expand Down Expand Up @@ -963,7 +968,6 @@ def get_active_line(self):
line = self.lines[ind]
return line


def get_active_linestyle(self):
'get the active lineinestyle'
ind = self.cbox_linestyles.get_active()
Expand Down Expand Up @@ -997,8 +1001,6 @@ def _update(self):

line.figure.canvas.draw()



def on_combobox_lineprops_changed(self, item):
'update the widgets from the active line'
if not self._inited: return
Expand Down Expand Up @@ -1044,17 +1046,14 @@ def on_dialog_lineprops_okbutton_clicked(self, button):
def on_dialog_lineprops_cancelbutton_clicked(self, button):
self.dlg.hide()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this exception handling? At the very least we could use the verbose.report() there to provide us information and then re-raise (or maybe add a message to the exception and let it bubble up?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment needs removing, as far as I can see, this try block will never fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually agree that this exception handling is unnecessary -- it really shouldn't fail here, unless something is seriously wrong. This is different from the try/excepts above where if the icon can't be set we should continue along without an icon.

# set icon used when windows are minimized
try:

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


def error_msg_gtk(msg, parent=None):
if parent is not None: # find the toplevel Gtk.Window
Expand Down
15 changes: 8 additions & 7 deletions lib/matplotlib/backends/backend_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ def new_figure_manager(num, *args, **kwargs):
figure = FigureClass(*args, **kwargs)
window = Tk.Tk()

# put a mpl icon on the window rather than the default tk icon. Tkinter
# doesn't allow colour icons on linux systems, but tk >=8.5 has a iconphoto
# command which we call directly. Source:
# http://mail.python.org/pipermail/tkinter-discuss/2006-November/000954.html
icon_fname = os.path.join(rcParams['datapath'], 'images', 'logo.gif')
icon_img = Tk.PhotoImage(file=icon_fname)
window.tk.call('wm', 'iconphoto', window._w, icon_img)
if Tk.TkVersion >= 8.5:
# put a mpl icon on the window rather than the default tk icon. Tkinter
# doesn't allow colour icons on linux systems, but tk >=8.5 has a iconphoto
# command which we call directly. Source:
# http://mail.python.org/pipermail/tkinter-discuss/2006-November/000954.html
icon_fname = os.path.join(rcParams['datapath'], 'images', 'matplotlib.gif')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like matplotlib.gif didn't get committed. I tried changing this to .png -- it appears that Tkinter doesn't support png, so we'll need to include a gif version of the regular matplotlib icon.

icon_img = Tk.PhotoImage(file=icon_fname)
window.tk.call('wm', 'iconphoto', window._w, icon_img)

canvas = FigureCanvasTkAgg(figure, master=window)
figManager = FigureManagerTkAgg(canvas, num, window)
Expand Down