user2981176
user2981176

Reputation: 43

Simple Image viewer

I am new to this site, and I am trying to create a simple image viewer in Python 2.7 using Tkinter, But when I try to load an image in it,it does not show anything!, I bet it is something embarassingly obvious, but I dont know what's wrong. I am using Windows XP. Here's my code:

from Tkinter import *
import tkFileDialog
from PIL import ImageTk, Image

root = Tk(className="Image viewer")

canvas_width = 800
canvas_height = 600
root.config(bg="white")

def openimage():
    picfile = tkFileDialog.askopenfilename()
    img = ImageTk.PhotoImage(file=picfile)
    canvas.create_image(0,0, anchor=NW, image=img) 

yscrollbar = Scrollbar(root)
yscrollbar.pack(side=RIGHT, fill=Y)

xscrollbar = Scrollbar(root, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)

canvas = Canvas(root, width=canvas_width, height=canvas_height, yscrollcommand=yscrollbar.set, xscrollcommand=xscrollbar.set)
button = Button(root,text="Open",command=openimage)
button.pack(side=BOTTOM)
canvas.pack(side=TOP)
yscrollbar.config(command=canvas.yview)
xscrollbar.config(command=canvas.xview)

mainloop()

Update: It works when i remove the file browser, and give it the path to a file, but i want the file browser, and using a label works, but scroll bars dont work with it, and i want to be able to scroll the picture.

Upvotes: 4

Views: 8378

Answers (2)

furas
furas

Reputation: 143097

I found on "The Tkinter PhotoImage Class" that PhotoImage can't be assigned to local variable in function because garbage collector remove it.

So you can use global variable:

img = None

def openimage():
    global img

    picfile = tkFileDialog.askopenfilename()
    img = ImageTk.PhotoImage(file=picfile)
    canvas.create_image(0,0, anchor=NW, image=img) 

or assign image to existing widget (for example canvas)

def openimage():
    picfile = tkFileDialog.askopenfilename()
    canvas.img = ImageTk.PhotoImage(file=picfile)
    canvas.create_image(0,0, anchor=NW, image=canvas.img) 

by the way: you should check if file was selected

def openimage():
    picfile = tkFileDialog.askopenfilename()
    if picfile:
        canvas.img = ImageTk.PhotoImage(file=picfile)
        canvas.create_image(0,0, anchor=NW, image=canvas.img) 

add scrollregion and you have file viewer with working scrollbars

def openimage():
    picfile = tkFileDialog.askopenfilename()
    if picfile:
        canvas.img = ImageTk.PhotoImage(file=picfile)
        canvas.create_image(0,0, anchor=NW, image=canvas.img) 
        canvas.configure(canvas, scrollregion=(0,0,canvas.img.width(),canvas.img.height()))

Upvotes: 4

xor
xor

Reputation: 2698

Dont know about problem in your code but You can use this function in place of your one:

def openimage():
    try:
        Artwork.destroy()
    except Exception:
        pass
    picfile = tkFileDialog.askopenfilename()
    img = ImageTk.PhotoImage(file=picfile)
    #canvas.create_image(0,0, anchor=NW, image=img)
    Artwork=Label(root,image=img)
    Artwork.img=img
    Artwork.pack(side=BOTTOM)#do packing urself


Note that is is the minimal implementation.

Upvotes: 0

Related Questions