首页 > 解决方案 > 如何更新脑切片的 Tkinter 图中的值,以便能够从 4D 图像中翻阅不同的脑容量

问题描述

我正在尝试设计一个 python gui,以便能够通过绘制大脑切片、逐帧位移时间序列和运动检测算法的不同输出来评估运动的影响。我希望能够单独滑过每个大脑体积(每次扫描 180 个体积),以便我可以将 FD 时间过程与实际大脑数据的样子进行比较。

我一直在使用 tkinter,我可以绘制一个脑容量的多个切片,但我正在更新选定的容量。我尝试创建按钮来前进和后退,并且还使用了 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()

目前我收到一个错误“应用程序没有属性'vol_scale'”,即使我已经在上面定义了它。

标签: pythontkinter

解决方案


推荐阅读