How to continuously change an image using the value of a tkinter Scale widget

756 Views Asked by At

I have a bunch of images which I want the user to select using a scale. As the user updates the scale value, I want the GUI to update which image is shown.

I have just started dealing with GUIs and I'm stuck. I've managed to print the new values from the scale using the command keyword argument of the Scale widget. However, it is not clear how I can get this value to update the image on the interface.

class MainProgram():

def AcquireDicomFiles(self):

    # HERE I GET THE IMAGES PATHS. IT WORKS FINE.


def GUI(self):

    root_window = Tk()

    slice_number = DoubleVar()
    bar_length = 200

    main_title = Label(root_window, text="Seleção de corte e echo").pack()

    scale_slice = Scale(root_window, variable=slice_number, orient=HORIZONTAL, from_=1, to=24, length=bar_length,
                  cursor="hand", label="Slice Number", command=MainProgram().get_slice_value)
    scale_slice.pack(anchor=CENTER)

    echo_time = DoubleVar()
    scale_echo = Scale(root_window, variable=echo_time, orient=HORIZONTAL, from_=1, to=6, length=bar_length,
                  cursor="hand", label="Echo Time")
    scale_echo.pack(anchor=CENTER)

    imagefile = Image.open("image.png")
    tk_image = ImageTk.PhotoImage(imagefile)

    panel = Label(root_window, image=tk_image).pack(fill="both", expand="yes")

    root_window.mainloop()

def get_slice_number(self,user_slice_number):
    user_slice_number = np.int(user_slice_number)

def showImage(self): 

    # Code below works fine for user input in terminal. It uses user_slice_number and user_echo_time to find image. I want these two values to come from the pair of scales

    # indexes = np.where(slice_and_echo[:][0] == user_slice_number)[0] #indexes of the elements where the user input match the element

    # for i in indexes: #Go through index values. Check and record those in which echo time (element) matches user input
    #     if slice_and_echo[i][1] == user_echo_time:
    #         selected_echo_time = user_echo_time
    #         selected_slice_number = slice_and_echo[i][0]
    #         index = i

    # file_path = os.path.join(dcm_folder, dcm_files[index]) #path of the file whose index match user input
    # dcm_read = dicom.read_file(file_path) #read file user wants
    # dcm_pixel_values = dcm_read.pixel_array #extract pixel values


slice_and_echo = MainProgram().AcquireDicomFiles()

MainProgram().GUI()
1

There are 1 best solutions below

1
On

Set echo_time.trace(slider_callback), (which calls the method given to it, whenever the echo_time changes value) and then within slider_callback, you set root_window.iconify(file_name).