I am trying to show different slices of an image using entry points in tkinter. However, when i enter a new slice number it is creating the image under the previous one. I dont know how to remove the first one to make space for the 2nd one. A
The code is the following
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
import numpy.ma as ma
import cv2
from os import listdir
from os.path import isfile, join
import ellipse as el
class mclass:
def __init__(self, window):
self.window = window
self.box = Entry(window)
self.button = Button (window, text="check", command=self.plot)
self.box.pack ()
self.button.pack()
def plot (self):
mypath='C:\\Users\\mehmet\\Desktop\\a1'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = np.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
images[n] = cv2.imread( join(mypath,onlyfiles[n]),cv2.IMREAD_GRAYSCALE)
image = np.stack([images[i] for i in range(13,299)])
arr_size = (265,490,286)
sphere_center = (120,238,76)
a=11
b=10
c=12
sphere = el.create_bin_sphere(arr_size,sphere_center, a,b,c)
sphere1=255*sphere.astype(np.uint8)
sphere2=np.swapaxes(sphere1,0,2)
dst = cv2.bitwise_or(sphere2, image)
img_p=dst[:,:,int(self.box.get())]
fig = Figure(figsize=(3,3))
a = fig.add_subplot(111)
a.imshow(img_p,cmap='gray')
a.plot()
canvas = FigureCanvasTkAgg(fig, master=self.window)
canvas.get_tk_widget().pack(side="top")
canvas.draw()
window= Tk()
start= mclass (window)
window.mainloop()
Related
I am having problems using matplotlib and tkinter at the same time.
I am trying to create a matplot graphic with radio buttons and embed it in tkinter
Following some examples and documentation over the Internet, I have created the following code:
import random
import matplotlib
import tkinter as Tk
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use('TkAgg')
root = Tk.Tk()
root.wm_title("Embedding in TK")
class TKInterGUI():
def __init__(self, master,fig):
self.fig = fig
self.master = master
def test(self):
canvas = FigureCanvasTkAgg(self.fig[0], self.master)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
ax = self.fig[0].add_axes([0.10, 0.7, 0.15, 0.15],facecolor='yellow')
r = RadioButtons(ax, ('2 Hz', '4 Hz', '0 Hz'))
fig = []
fig.append(plt.Figure(figsize=(5,5), dpi=100))
my_gui = TKInterGUI(root,fig)
my_gui.test()
Tk.mainloop()
This code generate the graphic and the radio buttons like intended. BUT the radio buttons do not work. They get completely irresponsive.
Now if I change the radio Button code to the main program like the code bellow, it all works fine:
import random
import matplotlib
import tkinter as Tk
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use('TkAgg')
root = Tk.Tk()
root.wm_title("Embedding in TK")
class TKInterGUI():
def __init__(self, master,fig):
self.fig = fig
self.master = master
def test(self):
canvas = FigureCanvasTkAgg(self.fig[0], self.master)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
fig = []
fig.append(plt.Figure(figsize=(5,5), dpi=100))
my_gui = TKInterGUI(root,fig)
my_gui.test()
ax = fig[0].add_axes([0.10, 0.7, 0.15, 0.15],facecolor='yellow')
r = RadioButtons(ax, ('2 Hz', '4 Hz', '0 Hz'))
Tk.mainloop()
Can anyone explain why the first code does not work, but the second one does?
I am trying to design a python gui to be able to assess impacts of motion by plotting brain slices, the framewise displacement timeseries, and different outputs of motion detection algorithms. I want to be able to slide through each of the brain volumes individually (180 volumes per scan) so that I can compare the FD timecourse to what the actual brain data looks like.
I've been using tkinter and I can plot several slices of one brain volume, but I'm having updating volume that is selected. I've tried creating buttons to advance and go back, and also using a tkinter Scale.
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
import os
import nibabel
from nilearn import plotting
from nilearn import image
from matplotlib.widgets import Slider, Button, RadioButtons
data_path = os.getcwd()
file='sub-HV01baseline_task-EA1_bold.nii'
file_path = os.path.join(data_path,file)
EA1=nibabel.load(file_path)
struct_arr2 = EA1.get_data()
vol=1
import tkinter as tk
from tkinter import ttk
fig = plt.Figure(figsize=(10,5), dpi=100)
class App:
def __init__(self, master):
self.event_num = 1
frame = tk.Frame(master)
frame.pack()
self.txt = tk.Entry(frame,width=10)
self.txt.pack(side="bottom")
self.lbl = tk.Label(frame, text="FD Value")
self.lbl.pack(side="bottom")
self.btn = tk.Button(frame, text = "Update",command=self.clicked)
self.btn.pack(side="bottom")
self.txt.focus()
self.var =tk.IntVar(frame)
self.var.set(0)
self.vol_scale=tk.Scale(frame,from_=0, to=180,orient="horizontal",sliderlength=20,command=self.show_slices(fig))
self.increase_btn = tk.Button(frame, text = "Increase",command=self.show_slices(fig))
self.increase_btn.pack(side="bottom")
self.vol_scale.pack(side="bottom")
#self.spin = tk.Spinbox(frame, from_=0, to=180, width=5, textvariable=self.var)
#self.spin.pack(side="bottom")
self.canvas = FigureCanvasTkAgg(fig,master=master)
self.canvas.get_tk_widget().pack(side=tk.TOP)
def clicked(self):
res = "FD = " + self.txt.get()
self.lbl.configure(text = res)
def show_slices(self,fig):
vol = self.vol_scale.get()
slice_0 = struct_arr2[:, :, 10,vol]
slice_1 = struct_arr2[:, : , 15,vol]
slice_2 = struct_arr2[:, :, 20,vol]
slice_3 = struct_arr2[:, :, 25,vol]
slice_4 = struct_arr2[:, : , 30,vol]
slices=[slice_0, slice_1, slice_2, slice_3, slice_4]
axes = fig.subplots(1, len(slices))
#svol = Slider(axes, 'Vol', 0, 180, valinit=0, valstep=1)
fig.subplots_adjust(hspace=0, wspace=0)
for i, slice in enumerate(slices):
axes[i].xaxis.set_major_locator(plt.NullLocator())
axes[i].yaxis.set_major_locator(plt.NullLocator())
axes[i].imshow(slice.T, origin="lower")
root=tk.Tk()
app = App(root)
root.mainloop()
Currently I'm getting an error that "App has no attribute 'vol_scale'" even though I've defined it above.
I am trying to display a GUI with a heatmap and scales/sliders with the scales/sliders changing the values in the heatmap.
I can display the heatmap and sliders and can read from the sliders but I cannot get the heat map to update after I have moved the sliders.
I have tried putting the code (I think) updates the heatmap in a function which is called whenever the scale/slider is moved but I am clearly missing something.
import tkinter
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def myFunc(value):
print (mySlider.get())
array[1][2]=mySlider.get()
#I think I need to put something here to update the heatmap when the
#scale/slider is changed but do not know what
figure, ax = plt.subplots()
ax.imshow(array)
canvas.get_tk_widget().pack()
root = tkinter.Tk()
root.title("Something")
array = ([[1,2,3,4],
[3,9,1,5],
[8,4,1,7],
[2,4,9,1]])
figure, ax = plt.subplots()
ax.imshow(array)
canvas = plt.Figure()
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().pack()
mySlider = tkinter.Scale(root, from_=0, to=15, orient=HORIZONTAL, command=myFunc)
mySlider.pack()
Like this:
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def myFunc(value):
print (mySlider.get())
array[1][2]=mySlider.get()
im.set_array(array)
canvas.draw()
root = tk.Tk()
root.title("Something")
array = ([[1,2,3,4],
[3,9,1,5],
[8,4,1,7],
[2,4,9,1]])
figure, ax = plt.subplots()
im = ax.imshow(array)
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().pack()
mySlider = tk.Scale(root, from_=0, to=15, orient=tk.HORIZONTAL, command=myFunc)
mySlider.pack()
root.mainloop()
However tkinter is not needed here. matplotlib has a slider built in (I assume you know since you imported it) which is a lot easier to implement:
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
def myFunc(value):
array[1][2]=value
im.set_array(array)
array = ([[1,2,3,4],
[3,9,1,5],
[8,4,1,7],
[2,4,9,1]])
figure, ax = plt.subplots()
im = ax.imshow(array)
ax_slider = plt.axes([0.1, 0.1, 0.8, 0.03]) # [left, bottom, width, height]
slide = Slider(ax_slider, '', 0, 15, valinit=0)
slide.on_changed(myFunc)
plt.show()
I'm working on curve fitting using the Radial Basis Neural Network. I have succeeded in embedding my plot to the tkinter canvas after following this guide.
My problem however is that when I run my code, the plot does not display in the canvas unless I either expand or contract the GUI window. I have searched for related problems here but I couldn't find one addressing specifically this issue. I am using python 3.4.2.
from tkinter import *
import math
import numpy as np
from numpy import *
import scipy
import scipy.linalg
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
class RadialBasisNetwork:
def __init__(self):
window = Tk()
window.title("RBF Curve Fitting")
self.f = Figure(figsize=(4,3), dpi=100)
self.canvas = FigureCanvasTkAgg(self.f, master=window)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=LEFT, fill=BOTH, expand=1)
self.canvas._tkcanvas.pack(side=LEFT, fill=BOTH, expand=1)
frame0 = Frame(window)
frame0.pack(fill=BOTH, expand=1)
title = Label(frame0, text = "RBF Network Controller", font = "Times 12 bold")
title.grid(row = 1, column = 1)
def Plot(self):
x = np.linspace(x_0, x_1, num = steps) # generate domain of x
plot_title = "model of " + model
a = self.f.add_subplot(111)
a.plot(x,ypred, 'r--') # plot x against predicted y
a.plot(x,y) # plot x against actual y (target)
a.legend(["Fit", "Target"], loc = "best", frameon = False, labelspacing = 0)
a.set_title(plot_title)
a.set_ylabel('Y axis')
a.set_xlabel('X axis')
RadialBasisNetwork()
I am writing a program that does some work and uses matplotlib to plot some data. This can take some time so I set up a progressbar using tkinter. Threading with tkinter was not that easy. I am running the progressbar in the main thread and my working stuff in a substhread. However I can not close the progressbar window after my work is done, because apparently matplotlib does something in the tk root window. I don't know what. I added a minimal example of what I am trying to do. Notice that removing the line "plotsomething()" makes it do what I want: close the progressbar after work is done.
Can you please help me figuring out how I can close the progressbar window without closing the matplotlib windows?
# coding = utf-8
import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk
import threading, queue
import time
def MAIN():
PB = q.get()
for i in np.arange(10):
time.sleep(0.2)
print(i)
PB.step(10)
PB.update()
print("Done")
def plotsomething():
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y)
root = tk.Tk()
root.title("Progress")
PB = ttk.Progressbar(root, orient = "horizontal",length=300, mode = 'determinate')
PB.pack()
q = queue.Queue()
q.put(PB)
plotsomething()
T = threading.Thread(target=MAIN(), name="MAIN")
T.start()
T.join()
plt.show()
EDIT - SOLUTION: I am solving the problem now by drawing every window seperatly by using the matplotlib tk backend. Apparently PyPlot is interfering with the tkinter root windows. See tcaswell's comment for more details and hints. Thank you very much!
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
import queue, threading, time
def center_window(window_parent, w=300, h=20):
# get screen width and height
ws = window_parent.winfo_screenwidth()
hs = window_parent.winfo_screenheight()
# calculate position x, y
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
window_parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
def MAIN():
PB = q.get()
for i in np.arange(10):
time.sleep(0.2)
print(i)
PB.step(10)
PB.update()
print("Done")
root = tk.Tk()
root.wm_title("Embedding in TK")
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a.plot(t,s)
a.set_title('Tk embedding')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')
#a tk.DrawingArea
root2 = tk.Tk()
PB = ttk.Progressbar(root2, orient = "horizontal",length=300, mode = 'determinate')
PB.pack()
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
root2.iconify()
root2.update()
root2.deiconify()
center_window(root2)
q = queue.Queue()
q.put(PB)
T = threading.Thread(target=MAIN(), name="MAIN")
T.start()
T.join()
root2.quit()
root2.destroy()
tk.mainloop()
You are getting conflicting gui-main loops between the TK you are starting and the TK that plt is start. If you want to use matplotlib with your own gui, you must embed it your self and you can not import pyplot. All the behind the scenes magic that makes the pyplot interface wonderful is what is messing you up here.
For a tutorial see here, for how mpl does the embedding see here.
Also see:
Unable to save matplotlib.figure Figure, canvas is None
Python Matplotlib runtime error upon closing the console
Matplotlib bar chart in a wx Frame instead of a new window