You can but you must take care that reference exist for the widgets here is Vegaseats example of toggle button with these warnings: http://www.daniweb.com/software-development/python/threads/191210/1512550#post1512550
Could be adapted to multiple previews of image. Notice that button does not need to be declared global in toggle_image, but it is defined in main code. Usually it is bad idea to combine creating widgets and showing info, as you end up creating zillion widgets instead of one.
Try to have patience, I get myself it mixed up!
P.S. I do have my shorter and without magic string version of the toggle_image as shown in following code adapted for askopenfilename.
What it means, is that you must use the global if you create the PhotoImage in function (or class)
# create Tkinter image objects and display them ...
# create all image objects in __main__ to be persistent
# Note: if you create the image object and tie it to a
# function scope variable, then when the function is done
# the image will be garbage collected
try:
# Python2
import Tkinter as tk
from tkFileDialog import askopenfilename
except ImportError:
# Python3
import tkinter as tk
from tkinter.filedialog import askopenfilename
def show_image(event):
global im
im = tk.PhotoImage(file=askopenfilename(filetypes=[('GIF FILES', '*.gif')]))
button['image'] = im
root = tk.Tk()
root.title("Click the image button")
# pick images you have in the working directory
# or give full path
# create a button to display the image
button = tk.Button(root, relief='raised', bd=10)
button.grid(row=1, column=1, padx=100, pady=10)
# bind left mouse click
button.bind('<Button-1>', show_image)
root.mainloop()
Or as function is object:
def show_image(event):
show_image.im = tk.PhotoImage(file=askopenfilename(filetypes=[('GIF FILES', '*.gif')]))
button['image'] = show_image.im
Here the class code again with withdrawing during download Toplevel window:
try:
import Tkinter as tk
from tkFileDialog import askopenfilename
except ImportError:
import tkinter as tk
from tkinter.filedialog import askopenfilename
class Uploader(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.root = tk.Canvas()
self.root.grid()
tk.Button(self.root, text='Upload an image',
command=self.get_image).grid()
tk.Label(text='Click preview picture to upload').grid()
self.preview = tk.Toplevel()
self.preview.withdraw()
self.upload = tk.Button(self.preview, command=self.upload)
self.upload.grid()
def get_image(self):
file_name=askopenfilename(filetypes=[('GIF FILES', '*.gif')])
self.preview.deiconify()
self.image= tk.PhotoImage(file=file_name) # critical save
self.upload['image'] = self.image
def upload(self):
self.upload['command'] = None
self.preview.withdraw()
print('withdrawing and uploading')
self.title('Uploading')
app = Uploader()
app.mainloop()