0

I created a tkinter frame with menus. The frame contains a button that generates a matplotlib figure window. When the figure window opens, all of the menus for the frame disappear (at least on a mac).

A minimal code example is shown below.

Any ideas would be much appreciated. Thanks!

#!/usr/bin/python

import matplotlib.pyplot as plt

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk
else:
    import tkinter as tk


class a_window_with_a_menubar:
    def __init__(self):
        self.root = tk.Tk()                

        self.new_fig_button = tk.Button(master = self.root, text="Make figure", command = self.make_figure)
        self.new_fig_button.grid(row = 0, column = 0, sticky = tk.W)


    def make_figure(self):
        self.fig1 = plt.figure(facecolor = 'white')
        self.ax1 = self.fig1.add_subplot(111)  
        self.ax1.cla()

        plt.show()


win = a_window_with_a_menubar()
tk.mainloop()

Note: the menus disappear whether I use the default tkinter menus or create my own.

emj
  • 45
  • 4
  • `matplotlib` use `tkinter` to display own window when you run `show()` but it has functions to "embed" plot inside your window - you can find it in many tutorials. – furas Jan 07 '17 at 06:06
  • BTW: your example doesn't have menubar so how we can test it ? – furas Jan 07 '17 at 06:10
  • `matplot` can create own `Tk()` and own `mainloop()` and it can make problem with your `Tk()` and your `mainloop()`. `tkinter` should have only one `Tk()` and only one `mainloop()`. – furas Jan 07 '17 at 06:15
  • Have you tried setting the TkAgg backend explicitly? http://stackoverflow.com/questions/32019556/matplotlib-crashing-tkinter-application/34109240 – DonCristobal Jan 07 '17 at 09:05
  • Thanks very much for the quick response! Sorry, I'm a little new to stackoverflow and I thought I'd get an email alert if anyone responded to my question. Thanks very much, furas and DonCristobal. To answer your questions, @furas, I tried embedding the matplotlib figure in a tkinter window, but I had the same problem with the menubar disappearing. I didn't explicitly set up a menubar in the example (I left it out to keep the code simple), but python creates a menubar by default when the program runs and this menubar disappears when matplotlib runs. – emj Jan 20 '17 at 05:01
  • @furas: The point about tkinter having only one Tk() and mainloop() is well taken. I'm just not sure how to deal with that issue. But thanks! – emj Jan 20 '17 at 05:01
  • @DonCristobal: Thanks very much for the suggestion. I believe I saw your other answer in my searches before and tried it. It does indeed change how the menu behaves. So far so good! However, my original menu still disappears and is replaced with a default python menubar. It seems like something along these lines might work though, so I'll explore this further. For now, I've resorted to a bit of a hack in which I simply regenerate the menus after plotting. It's ugly, but it will do for now. Thanks to both of you for your suggestions! – emj Jan 20 '17 at 05:08
  • maybe try `self.root = tk.Toplevel()`. `Toplevel` is used to create second/third window – furas Jan 20 '17 at 11:47
  • Thanks @furas. That's a good suggestion. Unfortunately, the menu still disappears. I don't think this would be a problem on a pc where the menubar is part of the window, but on a mac, matplotlib is replacing the menubar with its own as you pointed out, even when it's embedded in another window. My hack is working okay at the moment, but I have a few more things to try and will post them if they work. Thanks! – emj Jan 20 '17 at 17:47

0 Answers0