3

When I insert a matplotlib figure in my tkinter window, when I start my program, extra popup windows appear. They do not affect the functionality of my GUI, but they are a bit annoying.

I have written a basic script that shows the problem. I run this through Spyder:

import tkinter as tk
import matplotlib
matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.pyplot import figure as Figure
from matplotlib import pyplot as plt

class MyGUI(tk.Tk):
    def __init__(self,master):
        self.f=Figure(figsize=(5,5),dpi=100)
        self.fig, self.ax= plt.subplots()

        self.canvas = FigureCanvasTkAgg(self.fig,master)


        self.toolbar=NavigationToolbar2Tk(self.canvas,master)
        #self.toolbar.update()
        self.canvas._tkcanvas.pack(padx=20, pady=20)

root =tk.Tk()
window=MyGUI(root)
root.mainloop()

When I run this, I get three windows. One is the root window that shows an empty graph and a toolbar (labeled 'tk'). This is the only window that I want. Then I get a 'Figure 1' window with a toolbar and a 'Figure 2' window with a graph and toolbar. My output when I run the demo code

From commenting out the second half of the init method, it looks like the problem comes from this part.

self.f=Figure(figsize=(5,5),dpi=100)
self.fig, self.ax= plt.subplots()

self.canvas = FigureCanvasTkAgg(self.fig,master)

However, I am quite new to object oriented programming and tkinter, and therefore not experienced enough to figure out what the mistake is. Any ideas?

1 Answer 1

3

You are creating two figures. One of them is created via pyplot. One shouldn't try to embedd a pyplot figure inside a custom GUI. Remove pyplot completely and create only a single figure.

import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure

class MyGUI(tk.Tk):
    def __init__(self,master):
        self.fig=Figure(figsize=(5,5),dpi=100)
        self.ax = self.fig.add_subplot(111)

        self.canvas = FigureCanvasTkAgg(self.fig,master)

        self.toolbar=NavigationToolbar2Tk(self.canvas,master)
        #self.toolbar.update()
        self.canvas._tkcanvas.pack(padx=20, pady=20)

root =tk.Tk()
window=MyGUI(root)
root.mainloop()
Sign up to request clarification or add additional context in comments.

4 Comments

Running your code gets rid of one pop up window, but the other remains. I'm also not sure what you mean by ' remove pyplot completely and create only a single figure'. I am generating the figure through pyplot, so how is that possible?
If you look at the code from my answer, you'll see that there is no pyplot to be seen anywhere - this is what I mean by "remove pyplot", i.e. just don't use it. As for the other figure, it might be a leftover from previous codes run in the same kernels maybe? Did you try closing the console or Spyder completely?
Restarting the kernel worked. Thanks very much! I don't understand why one shouldn't embed a pyplot figure inside a GUI. Having adapted your two lines after def init, the code seems to work well. Even in your answer, pyplot still exists through 'Figure'. Is there some fundamental reason why pyplot and GUIs shouldn't mix?
Oh sorry, I didn't pay attention to that line. It should of course read from matplotlib.figure import Figure. I edited the answer. The reason not to use pyplot when having your custom GUI is that you would have pyplot as well as your custom GUI trying to manage the figures. It is not a priori clear which one will succeed in the end, so you might end up with a code that often works, but sometimes not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.