1

I have code for an interactive plot, which allows viewing a 3D image through scrolling slice-wise with the mouse roll. It includes also a slide bar to adjust contrast.

I have been trying to embed this into a Tkinter GUI, for example with help of this sample code: https://matplotlib.org/examples/user_interfaces/embedding_in_tk.html

But I don't really understand where my code is supposed to go in there.

This is the application I currently have:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider

class IndexTracker(object):
    def __init__(self, ax, X):
        self.ax = ax
        ax.set_title('use scroll wheel to navigate images')
        self.X = X
        rows, cols, self.slices = X.shape
        self.ind = self.slices//2
        self.im = ax.imshow(self.X[:, :, self.ind], cmap='gray')
        self.update()

    def onscroll(self, event):
        print("%s %s" % (event.button, event.step))
        if event.button == 'up':
            self.ind = (self.ind + 1) % self.slices
        else:
            self.ind = (self.ind - 1) % self.slices        
        self.update()

    def contrast(self, event):
        print('Changing contrast')
        print(smax.val)
        self.im.set_clim([0,smax.val])        
        self.update()

    def update(self):
        self.im.set_data(self.X[:, :, self.ind])
        self.ax.set_ylabel('slice %s' % self.ind)
        self.im.axes.figure.canvas.draw()


##### Create some random volumetric data

im = np.array(np.random.rand(10,10,10))

##### Initialize Tracker object with the data and Slider

fig, ax = plt.subplots(1,1)    
axmax  = fig.add_axes([0.25, 0.01, 0.65, 0.03])
smax = Slider(axmax, 'Max', 0, np.max(im), valinit=50)
tracker = IndexTracker(ax, im)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
smax.on_changed(tracker.contrast)

plt.show()

I don't understand what is exacty that I need to embed into the Tkinter application, is it fig, or IndexTracker ? How do I replace fig.canvas.mpl_connect('scroll_event', tracker.onscroll) so it works within the TKinter GUI?

1 Answer 1

3

There is nothing special embedding this into tkinter - you create a FigureCanvasTkAgg object first, and then do the rest. The only thing you need to change is instead of plt, you need to use Figure which is shown in the sample you quoted.

import numpy as np
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

class IndexTracker(object):
    ...


import tkinter as tk
root = tk.Tk()
fig = Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(fill="both", expand=True)

im = np.array(np.random.rand(10,10,10))

ax = fig.subplots(1,1)

axmax  = fig.add_axes([0.25, 0.01, 0.65, 0.03])
smax = Slider(axmax, 'Max', 0, np.max(im), valinit=50)
tracker = IndexTracker(ax, im)
canvas.mpl_connect('scroll_event', tracker.onscroll)
canvas.mpl_connect('button_release_event', tracker.contrast) #add this for contrast change
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

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.