2016-07-31 19 views
0

我想在tkinter GUI中嵌入一个matplotlib窗口,但我无法使“滑块”小部件显示出来。事实上,下面的代码确实显示了一个嵌入式图像,但是如果您点击它,图像本身的行为就像一个滑块!在Tk窗口中嵌入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() 

任何帮助深表感谢试过!

回答

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() 
+0

谢谢你的回应,我想在这采用的是滑盖通过3D阵列将我Tkinter的GUI嵌入MPL窗口。此代码说明了我想要嵌入的内容http://pastebin.com/AxnUZ7EJ 这会在单击按钮时产生一个新窗口,并通过滑动条滚动数组。目前只有两个虚拟阵列 – jm22b

+0

,那么问题是什么? http://pastebin.com/AxnUZ7EJ正在为我工​​作。你检查过我的代码吗? –

+0

我的歉意,我没有检查你的代码,并认为它没有显示滑块,但在再次检查时,我可以看到它的确如此。不知道我第一次做了什么......感谢您的帮助 “root_”中的下划线有什么意义? – jm22b