0

I am using tkinter to create a visualization tool. This tool has an input panel which has a couple of different options such as a bar plot of births, a word cloud of names, etc. The goal of this project is to have a user click a visualization and have that visualization displayed in the middle of the GUI. After, they can click a different visualization and have that displayed instead in the same spot as the last visualization. This will allow the user to see many different visualizations, one by one, based on the data set that they uploaded.

I am currently using plt.savefig('foo.jpg', bbox_inches='tight') to save an image to the directory after one of the options is chosen and the visualization is made. Then I use:

image = Image.open("foo.jpg")
photo = ImageTk.PhotoImage(image)
label_rhss = Label(image=photo) 

to open the image into the label. Unfortunately, this never updates unless I close and reopen the application and then the new visualization is created in the desired place. But this is by no means a viable solution for any user, especially after adding many more visualization options.

Is there a way to make it that every time I create a visualization plot with tkinter, to have that visualization displayed in the middle panel. From my understanding, tkinter does not update when saving the picture to a file and then accessing that after. I have tried a couple of methods but none seems to work.

Will it be easier for me to change to a different GUI such as Spyder or maybe a method that can update every time I create a visualization and place it immediately in the desired label in tkinter since the saving method does not seem to work?

Edit: The code that I am using to create the visualizations is in the form of radio buttons such as

radiobutton4 = Radiobutton(
    root, 
    bg = 'blue', 
    command = ShowWordCloud, 
    text = 'WordCloud of Names', 
    variable = v,
    indicatoron = 0,
    value = 3
)

The command in the radio button then calls the code which generates the word cloud and saves it as foo.jpg. I cant get this NEW foo.jpg to update as the new label image.

I hope this clarifies the problem a bit!

  • Does this answer your question? [How to update the image of a Tkinter Label widget?](https://stackoverflow.com/questions/3482081/how-to-update-the-image-of-a-tkinter-label-widget) – quamrana Dec 27 '19 at 16:06
  • No, unless I am reading it incorrectly. All the other resources on tkinter are using predefined images before tkinter is run to swap the label, I have managed to do that, switching the label image to another image is not the problem. My issue is when I create a visualization while tkinter is running and I update foo.png, the picture does not update. It does not realize that the file has changed, thus I believe it is not actively checking the directory. Im not sure if Im explaining this well enough. My bad. If theres anyway I can further explain my issue, pleas tell me. – TheCheekyOne Dec 27 '19 at 16:18
  • 1
    I've just done the experiment with a modified version of the answer I link to which reloads the same image file when you press `Enter`. Except I change the contents of the image with an image editor and the image does indeed update to the new edited version of the image each time. – quamrana Dec 27 '19 at 16:52
  • 1
    You are completely correct!! I guess I am a bit illiterate. Thank you so much for your time and help this saved me! Im not sure though why this works and my function does not. It has the same logic, but I guess the method is not within the loop. That may have been the reason that it did not work. Thanks again! Not sure how to close this, I guess the mods will after a while? – TheCheekyOne Dec 27 '19 at 17:17

0 Answers0