简体   繁体   English

matplotlib 添加到 tkinter 中的窗格窗口

[英]matplotlib add to panedwindow in tkinter

I do not understand how correctly add matplotlib into panedwindow of tkinter.我不明白如何正确地将 matplotlib 添加到 tkinter 的窗格窗口中。 I want to have on one side list box and matplotlib interactive plot, on the other side I need to resize listbox and plot by dragging boundary between them我想在一侧有列表框和 matplotlib 交互式 plot,另一方面我需要通过拖动它们之间的边界来调整列表框和 plot 的大小

this is what is desirable这是可取的

I tried this but I cannot add correctly matplot lib to paned window我试过了,但我无法正确地将 matplot lib 添加到窗格 window

from tkinter import *

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

import numpy as np

root = Tk()

m = PanedWindow(root)
m.pack(fill=BOTH, expand=1)




fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))

canvas = FigureCanvasTkAgg(fig, master = root)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1)
m.add(toolbar)
m.add(canvas)



lstbox2 = Listbox(selectmode=MULTIPLE, width=20, height=10)
m.add(lstbox2) 

root.mainloop()

Put your canvas and toolbar in a Frame instead, and add that Frame to your paned window:将您的canvastoolbar放在一个Frame中,然后将该Frame添加到您的窗格 window:

root = Tk()

...

canvas_frame = Frame(root)
canvas = FigureCanvasTkAgg(fig, master = canvas_frame)
canvas.draw()
canvas.get_tk_widget().pack()

toolbar = NavigationToolbar2Tk(canvas, canvas_frame)
toolbar.update()
toolbar.pack_configure(expand=True)

m.add(canvas_frame)

lstbox2 = Listbox(selectmode=MULTIPLE, width=20, height=10)
m.add(lstbox2)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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