Wallpapering a wxPython background

vegaseat 1 Tallied Votes 976 Views Share

Wallpaper or tile images are small images that can be spread across a background repetitively. A common practice on web pages. The surface created with wx.PaintDC() can be used for this effect and still allows other widgets to be put on top of the wallpaper. The small tile image is included in the code, in the form of a base64 encoded image string.

# wallpaper/tile a wxPython panel using a base 64 encoded image string
"""
# the image string is created with this simple Python code 
# and the string is then copied and pasted to the program
import base64
jpg_file = "BG_Egypt.jpg"
print \
"jpg_b64='''\\\n" + base64.encodestring(open(jpg_file,"rb").read()) + "'''"

"""
# tested with Python24 and wxPython26    vegaseat     06mar2007

import wx
import cStringIO
import base64

BG_Egypt_jpg_b64='''\
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU
FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo
KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwB2taqm
lRI7pAsCxQDi0WVizqo7KzEln9+v55f/AAlsZ/5dp+f+oI//AMa9/wDPc8c8wW3f97p3v/HD9f8A
P66mtaqmlRI7pAsCxQDi0WVizqo7KzEln9+v5+J9+9tDQy/+EtjP/LtPz/1BH/8AjXv/AJ7x67q0
9xo8Lxp5AlurUc2ggk2mdAeCoYAgn6g9wea2r+Inv7aK3tILpZZLm3yV0t4fl85C2X8sYG3OeeQS
O/N3xzzBbd/3une/8cP1/wA/qbWab36gHjnmC27/AL3Tvf8Ajh+v+f1PHPMFt3/e6d7/AMcP1/z+
uXr2sDV1hRIb553urU86fLGoVJYz3QKAFH+e+hresafcXapJDfSiHyDxYTsvmRhCOQhDAMo9QfcH
k1te3W4Fu98UW0E/kQW8xWNEQkaQ7gsFAb5vLO75ieckHtnvia9rA1dYUSG+ed7q1POnyxqFSWM9
0CgBR/nvqf8ACWxn/l2n5/6gj/8Axr3/AM9z/hLYz/y7T8/9QR//AI17/wCe58mB/9k=
'''

jpg1 = base64.b64decode(BG_Egypt_jpg_b64)
# save the image to a file ...
# (couldn't figure out any other way to use with wx.Bitmap)
tile_file = "zzz777.jpg"
fout = open(tile_file, "wb")
fout.write(jpg1)
fout.close()

class Panel1(wx.Panel):
    """ 
    class Panel1 creates a panel for the tile image
    fw and fh are the width and height of the base frame
    """
    def __init__(self, parent, id, fw, fh, tile_file):
        # create the panel
        wx.Panel.__init__(self, parent, id)
        # frame/panel width and height
        self.fw = fw
        self.fh = fh
        # load the wallpaper/tile file
        self.bmp1 = wx.Bitmap(tile_file)
        # do the wall papering ...
        wx.EVT_PAINT(self, self.on_paint)
        # now put a button on the panel, on top of wallpaper
        self.button1 = wx.Button(self, -1, label='Button1', pos=(10, 5))        
        
    def on_paint(self, event=None):
        # create paint surface
        dc = wx.PaintDC(self)
        dc.Clear()
        #dc.SetBackgroundMode(wx.TRANSPARENT)
        # get image width and height
        iw = self.bmp1.GetWidth()
        ih = self.bmp1.GetHeight()
        # tile/wallpaper the image across the canvas
        for x in range(0, self.fw, iw):
            for y in range(0, self.fh, ih):
                dc.DrawBitmap(self.bmp1, x, y, True)


app = wx.PySimpleApp()
# create a window/frame instance, no parent, -1 is default ID
fw = 400
fh = 300
frame1 = wx.Frame(None, -1, "wall-papering a wx.PaintDC surface", size=(fw, fh))
# create a panel class instance
panel1 = Panel1(frame1, -1, fw, fh, tile_file)
frame1.Show(True)
# start the event loop
app.MainLoop()
kush87 0 Newbie Poster

what if you resize the window?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If you want to resize the window, you can use a slightly more modern approach as shown here:
http://www.daniweb.com/forums/post1119379.html#post1119379

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.