1

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.

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\raw\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()
martineau
  • 119,623
  • 25
  • 170
  • 301
SteveSmith
  • 19
  • 5
  • Another problem (besides the linked dup) is because you are setting the `command=` option to the image. That option is to specify a function to call when the `Button` is clicked. To put an image on a `Button`, use `image=` for that. Here's some documentation on [`Button`](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/button.html)s. – martineau Feb 11 '21 at 01:35
  • I don't know if you understand what I am trying to do here. I am not trying to put an image onto a button. I am trying to create a button to open an image inside the root window I have created. – SteveSmith Feb 12 '21 at 06:34
  • Oh. I see. However that doesn't change that what you're setting the `command` to is still wrong in two ways. First it's executing when you create the `Button` — see [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) and secondly because it should be (just) the name of a function to call when the `Button` is clicked. The return value of this callback function will be ignored. In your case it should open an image and put it inside the root window. – martineau Feb 12 '21 at 09:18
  • Note, too, that to put "an image inside the root window", you will need to make it part of some widget and `place()` that. The widgets commonly used to contain them are `Button`s, `Label`s, and `Canvas`es. – martineau Feb 12 '21 at 09:26

1 Answers1

0

There are a number of errors in your code and previously I closed it after picking one of them and marking it as a duplicate of another question that had been asked and answered before that covered that problem.

However, based on comments you made and after thinking it over, I decided to reopen it and attempt to address all or at least most of the issues I saw — otherwise it would likely have taken you quite a while to get everything fixed.

Here's the result:

from PIL import ImageTk, Image
import tkinter as tk

class ImageViewer:
    def __init__(self, root, image_filename):
        self.root = root
        self.image_filename = image_filename

        self.root.title('ImageViewer')
        self.root.geometry('400x350')

        self.canvas = tk.Canvas(self.root, width=300, height=300)
        self.canvas.place(x=10, y=10)

        self.btnView = tk.Button(text='View', command=self.view_image)
        self.btnView.place(x=20, y=265)

        self.btnClose = tk.Button(text='close', command=self.root.destroy)
        self.btnClose.place(x=65, y=265)

    def view_image(self):
        self.img = ImageTk.PhotoImage(Image.open(self.image_filename))  # Keep ref to image.
        self.canvas.create_image(20, 20, anchor=tk.NW, image=self.img)


def main(image_filename):
    root = tk.Tk()
    ImageViewer(root, image_filename)
    root.mainloop()

if __name__ == '__main__':
    main(r"C:\Users\SteveSmith\eclipse-workspace\SteveSmith-ex1\src\raw\pythonIsFun.jpg")
martineau
  • 119,623
  • 25
  • 170
  • 301
  • This is exactly what I was looking for. I greatly appreciate the help you have provided here. – SteveSmith Feb 12 '21 at 23:48
  • Steve: You're welcome. Hope starting with some working code helps with your project. `tkinter` is a difficult module and can be tricky to work with, so I decided to give you a little boost mastering it. – martineau Feb 13 '21 at 02:15