-1

I seem to be having some problems with this code I created. I am trying to get two buttons to create images on my Tkinter canvas.

For some reason my buttons when clicked do NOT RETURN ANY ERROR however they do not show the images either in the canvas.

Can anyone help me figure out what the error in my code is? Thanks in advance!

Note that when the code to create both images is run outside the defined functions it does succesfully create the images on the canvas, the problem is that the button does not execute the command correctly

import tkinter
from tkinter import ttk
from tkinter import *
import PIL.Image
import PIL.ImageTk

root = Tk()

canvas = Canvas(root)
canvas.pack()
canvas.config(width=300, height= 500,background = 'white')


def previewpng(): 
    photo = PhotoImage( file = 'C:/Users/Admin/Documents/logo.png')
    png_preview = canvas.create_image(150,150, image = photo)

def preview():
    Imagetif = PIL.Image.open( "C:/Users/Admin/Documents/sampledata.tif")
    Im = PIL.ImageTk.PhotoImage(Imagetif)
    preview = canvas.create_image( relx = 0, rely= 0, image = Im )


#creating buttons
button= ttk.Button (root, text= 'View preview of PNG')
button.pack()
button.config( command = previewpng )

button2= ttk.Button (root, text= 'View Preview of Tif file')
button2.pack()
button2.config( command = preview )



root.mainloop()

1 Answers1

0

You create the image inside a function, previewpng() and when the function exits the variable containing the image reference is garbage collected ant tkinter can no longer find it.

Add a line to save the image reference in each function:

def previewpng(): 
    photo = PhotoImage( file = 'pilner.png')
    png_preview = canvas.create_image(150,150, image = photo)
    canvas.image = photo  # Save image reference

def preview():
    Imagetif = PIL.Image.open( "beear.tif")
    Im = PIL.ImageTk.PhotoImage(Imagetif)
    preview = canvas.create_image(50, 50, image = Im )
    canvas.image2 = Im    # Save image reference
figbeam
  • 7,001
  • 2
  • 12
  • 18
  • Unfortunately when I run the code with your indication I get an Error as follows, any idea as to why? – Camilla Tac Jul 19 '18 at 15:49
  • `Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Camilla Tac\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "", line 22, in preview preview = canvas.create_image( relx = 0, rely= 0, image = Im ) File "C:\Users\Camilla Tac\Anaconda3\lib\tkinter\__init__.py", line 2483, in create_image return self._create('image', args, kw) File "C:\Users\Camilla Tac\Anaconda3\lib\tkinter\__init__.py", line 2467, in _create cnf = args[-1] IndexError: tuple index out of range` – Camilla Tac Jul 19 '18 at 15:50
  • The error is caused by the line `preview = canvas.create_image(relx=0, rely=0, image=Im )` as the function `create_image` wants the first two arguments to be the x and y positions. The function is called without these arguments and that causes the error. I changed my example accordingly. – figbeam Jul 19 '18 at 16:37