简体   繁体   中英

Matplotlib NavigationToolbar widget is not displayed in Tkinter GUI

I'm designing a simple GUI with Tkinter composed of a ListBox (with Scrollbar) and a canvas (FigureCanvasTkAgg) to plot. The ListBox is a collection of variables (time series) that are drawn as a xy plot upon selection. The layout is schematically:

|---------------- Menu Bar --------------|
|----------------------------------------|
| |--- Frame 1-- | |------ Frame 2 ------|
| |-- ListBox -- |^|                     |
| |  Item 1      |-|                     |
| |  Item 2      |-|       Canvas        |
| |  Item 3      |-|                     |
| |  Item 4      |-| ------------------- |
| |  Item 5      |-|  Navigation toolbar |
| |--------------|v|---------------------|
|----------------------------------------| 

When the GUI is built, the Navigation Toolbar is absent and I have tried either with both pack() and grid() geometry managers. I apologize if the mistake is so evident but I'm not aware of what can be wrong. The minimal code is:

import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from tkinter import *

class Plot_tool(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)            
        self.master = master
        self.master.option_add('*tearOff', 'FALSE')
        self.master.title("Tool")
        self.options = [ i for i in range(50) ] # Some staff in ListBox
        self.pack(fill = BOTH, expand = 1)
        self.init_window()

    def init_window(self):
        menubar = Menu(self.master)
        self.master.config(menu = menubar)

        filemenu = Menu(menubar)
        filemenu.add_command(label = "Load")
        menubar.add_cascade(label = "File", menu = filemenu)

        # Frame 1 (ListBox and Scrollbar)
        framei = LabelFrame(self, text="frame", labelanchor="n")
        framei.pack(side="left", fill=Y)

        scrollbar = Scrollbar(framei)
        scrollbar.pack(side="right",fill=Y)
        self.items = Listbox(framei, yscrollcommand=scrollbar.set)
        self.items.pack(side="left", fill=Y)
        scrollbar.config(command = self.items.yview)
        self.items.insert(0, *self.options)

        # Frame 2 (Canvas and Navigation Toolbar)
        group2 = Frame(self)
        group2.pack(side="right")

        fig = Figure(figsize=(200,100))
        self.eje = fig.add_subplot(111)
        self.figura = FigureCanvasTkAgg(fig, master = group2)
        self.figura.get_tk_widget().pack()
        self.figura.show()

        toolbar = NavigationToolbar2TkAgg(self.figura, group2)
        toolbar.pack()
        toolbar.update()

def on_closing(root):
    root.destroy()
    exit()

def main():
    root = Tk()
    root.geometry("900x500")
    app = Plot_tool(root)
    root.protocol("WM_DELETE_WINDOW", lambda: on_closing(root))
    root.mainloop()

if __name__ == '__main__':
    main()

Sticking to the embedding_in_tk example from the matplotlib page, you'd first create the toolbar and then pack the canvas.

    group2 = Frame(self)
    group2.pack(side="right")

    fig = Figure(figsize=(200,100))
    self.eje = fig.add_subplot(111)
    self.figura = FigureCanvasTkAgg(fig, master = group2)
    toolbar = NavigationToolbar2TkAgg(self.figura, group2)
    self.figura._tkcanvas.pack()
    toolbar.update()

Additionally, removing self.figura.show() seems to speed up things a lot.

Note: In newer versions of matplotlib you should use NavigationToolbar2Tk instead of NavigationToolbar2TkAgg .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM