How to make button command to open image inside window in tkinter?

SteveSmith

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

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")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Onclick Button open a new Window only with Image inside

From Dev

How to open a toplevel window with a button python 3/Tkinter

From Dev

How to make state of window idle until button is pressed in python tkinter?

From Dev

how to make only 1 new window when a button is clicked? tkinter

From Dev

How do I make this button quit the parent window in tkinter?

From Dev

how to make a tmux window remain open after running command

From Dev

Open Window Activate not working with button inside menu

From Dev

Tkinter - How can I make a button can create new window and close the main window

From Dev

How to make a Tkinter window not resizable?

From Dev

How to Make Button Tag Only Show Image Inside of it

From Dev

python tkinter open new window with button click and close first window

From Dev

How to make an Image button circular and inside the circle,an image will be shown and a border will be there around the image

From Dev

How to close tkinter window without a button?

From Dev

How can I make a batch script open a command-line window in it's current location?

From Dev

Make a new window open below the blinking cursor using Tkinter

From Dev

How to make a window fullscreen in a secondary display with tkinter?

From Dev

How to load an image into a python 3.4 tkinter window?

From Dev

Why can't I "open command window here" inside a Library?

From Dev

How to open an image by clicking the image button

From Dev

How to open a window without "close" button?

From Dev

How to open a window without "close" button?

From Dev

Python 3 tkinter button command inside different class

From Dev

How to make a button with an image background?

From Dev

How to make an only image button?

From Dev

Python Selenium: How to make browser window not open

From Dev

How to make my links open a 'popup' window

From Dev

Why does my button make my function open in a different window?

From Dev

How to make a Button using the tkinter Canvas widget?

From Dev

Tkinter: How to make a button center itself?

Related Related

  1. 1

    Onclick Button open a new Window only with Image inside

  2. 2

    How to open a toplevel window with a button python 3/Tkinter

  3. 3

    How to make state of window idle until button is pressed in python tkinter?

  4. 4

    how to make only 1 new window when a button is clicked? tkinter

  5. 5

    How do I make this button quit the parent window in tkinter?

  6. 6

    how to make a tmux window remain open after running command

  7. 7

    Open Window Activate not working with button inside menu

  8. 8

    Tkinter - How can I make a button can create new window and close the main window

  9. 9

    How to make a Tkinter window not resizable?

  10. 10

    How to Make Button Tag Only Show Image Inside of it

  11. 11

    python tkinter open new window with button click and close first window

  12. 12

    How to make an Image button circular and inside the circle,an image will be shown and a border will be there around the image

  13. 13

    How to close tkinter window without a button?

  14. 14

    How can I make a batch script open a command-line window in it's current location?

  15. 15

    Make a new window open below the blinking cursor using Tkinter

  16. 16

    How to make a window fullscreen in a secondary display with tkinter?

  17. 17

    How to load an image into a python 3.4 tkinter window?

  18. 18

    Why can't I "open command window here" inside a Library?

  19. 19

    How to open an image by clicking the image button

  20. 20

    How to open a window without "close" button?

  21. 21

    How to open a window without "close" button?

  22. 22

    Python 3 tkinter button command inside different class

  23. 23

    How to make a button with an image background?

  24. 24

    How to make an only image button?

  25. 25

    Python Selenium: How to make browser window not open

  26. 26

    How to make my links open a 'popup' window

  27. 27

    Why does my button make my function open in a different window?

  28. 28

    How to make a Button using the tkinter Canvas widget?

  29. 29

    Tkinter: How to make a button center itself?

HotTag

Archive