I need a way to make an image as a background for a window, and still be able to put buttons and other controls on top of it. I even tried using wxPython's wxPanel, but it proved a real pain because I found the documentation very sparce...:sad: (google didn't even help).

Is there anyway I can do this? I've heard about the canvas but I have no idea how to use this...


Recommended Answers

All 3 Replies

My new machine has Windows Vista OS and I can't get Tkinter to work, but wxPython works fine ...

# create a background image on a wxPython panel
# and show a button on top of the image
import wx
class Panel1(wx.Panel):
    """class Panel1 creates a panel with an image on it, inherits wx.Panel"""
    def __init__(self, parent, id):
        # create the panel
        wx.Panel.__init__(self, parent, id)
            # pick an image file you have in the working folder
            # you can load .jpg  .png  .bmp  or .gif files
            image_file = 'roses.jpg'
            bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            # image's upper left corner anchors at panel coordinates (0, 0)
            self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0, 0))
            # show some image details
            str1 = "%s  %dx%d" % (image_file, bmp1.GetWidth(), bmp1.GetHeight()) 
        except IOError:
            print "Image file %s not found" % imageFile
            raise SystemExit
        # button goes on the image --> self.bitmap1 is the parent
        self.button1 = wx.Button(self.bitmap1, id=-1, label='Button1', pos=(8, 8))

app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# change the size of the frame to fit the backgound images
frame1 = wx.Frame(None, -1, "An image on a panel", size=(350, 400))
# create the class instance
panel1 = Panel1(frame1, -1)

quick question. Can I use Tkinter and wxpython in the same app? (See i built this splash screen class in Tkinter --doesn't use buttons, but does a lot of work (loading images/files/wat not)).

If not, is there a way to remove the title bar and border in wxpython like


does in Tkinter?

No, you can not use Tkinter and wxPython in the same application. Their eventloops would clash.

Yes you can remove the title bar from a wx.Frame ...

A frame without titlebar:
You can change frame style to wxMINIMIZE_BOX, but now you have
to supply an exit button somewhere.

I finally doctored the Python25 Vista installation to make Tkinter work. Here is a similar code for Tkinter ...

# use a Tkinter label as a panel/frame with a background image
# (note that Tkinter reads only GIF and PGM/PPM images)
import Tkinter as tk
root = tk.Tk()
root.title('background image')
# pick a .gif image file you have in the working directory
image1 = tk.PhotoImage(file="roses.gif")
w = image1.width()
h = image1.height()
root.geometry("%dx%d+0+0" % (w, h))
# tk.Frame has no image argument
# so use a label as a panel/frame
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
button2 = tk.Button(panel1, text='button2')
# save the panel's image from 'garbage collection'
panel1.image = image1
# start the event loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.