Question: How to make button command to open image inside window in python tkinter? I am attempting to create a very simple image viewer in tkinter
How to make button command to open image inside window in python tkinter?
I am attempting to create a very simple image viewer in tkinter with two simple buttons view and close. I have the close button functioning as intended but I am unable to get my view button to open the specified image in my file directory. I have tried importing ImageTK to write a button command to open it but have so far been unsuccessful.
This is my code so far. Any help would be appreciated.
''' import tkinter as tk from PIL import ImageTk,Image class image_viewer: def __init__(self, win): self.root = win self.root.title('ImageViewer') self.root.geometry('400x350') self.btnView = tk.Button(text='View', command= ImageTk.PhotoImage(Image.open(r"C:\Users\SteveSmith\eclipse-workspace\SteveSmith-ex1\src aw\pythonIsFun.jpg"))) self.btnView.pack(side=tk.LEFT) self.btnView.place(x=20, y=265) self.btnClose = tk.Button(text='close', command=self.root.destroy) self.btnClose.pack(side=tk.LEFT) self.btnClose.place(x=65, y=265) def main(): root = tk.Tk() image_viewer(root) root.mainloop() if __name__ == '__main__': main() '''
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
