I'm writing a short script to display a user selected image in a canvas. The image the user selects could be different sizes. In the long run I want to have the application maximise and present scroll bars if the image loaded is larger than the screen resolution, however I have what is most likely a simple problem, the image selected isn't loaded into the Canvas. I just need another set of eyes.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import Tk, Canvas, Frame, Menu, BOTH, NW
import Image
import ImageTk
import tkFileDialog
appname = "example"
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title( appname )
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar, tearoff=0)
fileMenu.add_command(label="Open File", command=self.fileOpen)
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
def onExit(self):
self.quit()
def fileOpen(self):
file = tkFileDialog.askopenfile(
parent=self.parent,
mode='rb',
title='Choose a file',
filetypes=[ ( "Image files",("*.jpg", "*.jpeg" ) )] )
if file != None:
self.img = Image.open(file)
self.tatras = ImageTk.PhotoImage(self.img)
canvas = Canvas(self, width=self.img.size[0]+20,
height=self.img.size[1]+20)
canvas.create_image(10, 10, anchor=NW, image=self.tatras)
canvas.pack(fill=BOTH, expand=1)
def filePref(self):
self.quit()
def main():
root = Tk()
root.geometry("250x150+300+300")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Why doesn't the image load into the canvas when selected? I don't get an error displayed. How can I maxmimise the whole window if the image is larger than the initial window size? How can I add scroll bars if the image is larger than screen resolution?
Thanks