As part of a program I am working on I want to display a
48x48 XBM image on the main window.  I have done a lot
of searching for code snippets and I found what appears
to be the correct way to do it using a Label.  So far I
have not been able to get it to work.  I have tried
different variations of the syntax but nothing will
display the image.  The Label will expand to size of
the image but nothing is there.  I have the background
of the Label set to white so it can be seen.

For experimentation I create a small program for the
sole purpose of displaying an XBM image.  The complete
script is pasted below.  I am thinking the problem 
might have something with the way the image object
is being created.  I'm probable making a dumb newbie
mistake.  BTW, if I run the script from a terminal
window, I do not get any errors.  Any guidance is
appreciated.


#!/usr/bin/python

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

class cv():

    default_dir = os.environ['HOME']

class Window(Frame):

    def __init__(self, master = None):
        Frame.__init__(self,master)
        self.master = master
        self.init_window()

    def init_window(self):
        self.master.title("XBM Image Test")
        self.pack(fill=BOTH, expand=1)

        self.openButton = Button(self,
                                 text="Open Image",
                                 width = 10,
                                 command=self.open_image)
        self.openButton.place(x=50, y=100)

        self.quitButton = Button(self,
                                 text="Quit",
                                 width=10,
                                 command=self.quit)
        self.quitButton.place(x=50, y=140)

        self.xbmImage = Label(self,
                              bg="white")
        self.xbmImage.place(x=80, y=25)

    def open_image(self):
        file_filter = [
            ('X BitMap', '*.xbm *.XBM'),
            ('all files', '*.*')
            ]
        fileName = tkFileDialog.askopenfilename(parent=root,
                                                initialdir=cv.default_dir,
                                                filetypes=file_filter,
                                                title="Open XBM Image")
        if fileName:
            cv.default_dir = os.path.dirname(fileName)
            openImage = Image.open(fileName)
            imageFile = ImageTk.BitmapImage(openImage)
            self.xbmImage.config(image=imageFile)
        else:
             return None

root = Tk()
root.minsize(width=200, height=180)
root.maxsize(width=200, height=180)
app = Window(root)
root.mainloop()

-- 
<Wildman> GNU/Linux user #557453
May the Source be with you.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to