在Tk窗口中嵌入Matplotlib和Slider

时间:2016-07-31 11:47:41

标签: python python-2.7 matplotlib tkinter

我正在尝试在tkinter GUI中嵌入一个matplotlib窗口,但是我无法获得"滑块"要显示的小部件。实际上,下面的代码确实显示了嵌入的图像,但是如果单击它,图像本身的行为就像滑块一样!

我尝试过使用这个问题的想法

Placing plot on Tkinter main window in Python

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.widgets import Slider
import Tkinter as tk


class Embed:

    def __init__(self, root):

        self.root = root
        self.plot = Plotting(self.root)
        self.a = np.array([[1,2,3],[2,3,4],[3,4,5]])
        self.b = np.array([[1,2,3],[4,5,6],[7,8,9]])
        self.ctuple = (self.a,self.b)
        self.cube = np.dstack(self.ctuple)
        self.button = tk.Button(root, text="check", command=lambda: self.plot.plot(self.cube))
        self.button.pack()

class Plotting:

    def __init__(self, root):

        self.root = root

    def plot(self, cube, axis=2, **kwargs):

        fig = Figure(figsize=(6,6))
        ax = fig.add_subplot(111)
        s = [slice(0,1) if i == axis else slice(None) for i in xrange(3)]
        im = cube[s].squeeze()

        l = ax.imshow(im, **kwargs)
        canvas = FigureCanvasTkAgg(fig, self.root)
        canvas.get_tk_widget().pack()
        canvas.draw()
        slider = Slider(ax, 'Axis %i index' % axis, 0, cube.shape[axis] - 1,
                    valinit=0, valfmt='%i')

        def update(val):
            ind = int(slider.val)
            s = [slice(ind, ind + 1) if i == axis else slice(None)
             for i in xrange(3)]
            im = cube[s].squeeze()
            l.set_data(im, **kwargs)
            canvas.draw()

        slider.on_changed(update)

if __name__ == '__main__':

    root = tk.Tk()
    app = Embed(root)
    root.mainloop()
    root.destroy()

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我不知道你的代码是做什么的。我试图尽可能少地改变它:

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import Tkinter as Tk
import matplotlib.pyplot as plt


class Embed:

    def __init__(self, root_):
        self.root = root_
        self.a = np.array([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
        self.b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        self.c = np.array([[1, 4, 7], [4, 7, 10], [7, 10, 13]])
        self.ctuple = (self.a, self.b)
        self.cube = np.dstack(self.ctuple)
        self.cube = np.dstack([self.cube, self.c])
        self.plot = Plotting(self.root, self.cube)
        self.button = Tk.Button(root, text="check", command=lambda: self.plot.plot())
        self.button.pack()


class Plotting:
    def __init__(self, root, cube):
        self.root = root
        self.fig = Figure(figsize=(6, 6))
        self.ax = self.fig.add_subplot(111)
        self.cube = cube
        self.slider = Tk.Scale(
            self.root, from_=0, to=cube.shape[2] - 1, resolution=0.01, orient=Tk.HORIZONTAL, command=self.update
        )
        # self.slider.on_changed(self.update)
        self.slider.pack()
        self.plotted = False
        self.l = None
        self.canvas = None
        plt.show()

    def plot(self, **kwargs):
        self.plotted = True
        s = [slice(0, 1) if i == 2 else slice(None) for i in xrange(3)]
        im = self.cube[s].squeeze()
        self.l = self.ax.imshow(im, **kwargs)
        self.canvas = FigureCanvasTkAgg(self.fig, self.root)
        self.canvas.get_tk_widget().pack()
        self.canvas.draw()

    def update(self, val):
        ind = int(self.slider.get())
        s = [slice(ind, ind + 1) if i == 2 else slice(None) for i in xrange(3)]
        if self.plotted:
            im = self.cube[s].squeeze()
            self.l.set_data(im)
            self.canvas.draw()


if __name__ == '__main__':
    root = Tk.Tk()
    app = Embed(root)
    root.mainloop()
    root.destroy()