Adding a tileable background image to a frame

I'm a newbie to wxPython and I've been struggling with something for several days now and was hoping maybe someone out there could help me with something.

I want to add a background image to one of my frames. I can add a static image fine. But when I resize the window, the image gets distorted. So what I really want to do is use a tileable image. In addition, I want to be able to add other object on top of the image, and I can't figure out how to do that. I'm a pretty novice programmer, so there is probably a very simple answer to this question, but I've been beating my head against the wall for days on this! The way I'm thinking about this, is I want to do what you do with a webpage, where you set the background image for the page, and then you build on top of that. Can this be done in wxPython?

Any help is GREATLY appreciated!

Kim

I hope this isn't terrible as I'm a few drinks in already (hey, it's
Thursday!). The Draw() method could undoubtedly be optimized.

from wxPython.wx import *

class TiledPanel(wxPanel):
    def __init__(self, parent, id, image):
        wxPanel.__init__(self, parent, id)
        self.image = wxBitmapFromImage(image)
        self.height, self.width = image.GetHeight(), image.GetWidth()
        EVT_PAINT(self, self.OnPaint)

    def OnPaint(self, event):
        self.Draw(wxPaintDC(self))

    def Draw(self, dc):
        h, w = self.GetSize()
        cols = w / self.width + 1 or 1
        rows = h / self.height + 1 or 1
        x = y = 0
        for r in range(rows):
            for c in range(cols):
                dc.DrawBitmap(self.image, y, x, True)
                x += self.width
            y += self.height
            x = 0

class Frame(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self, parent, id, title)
        image = wxImage('/usr/share/pixmaps/gnome-home.png',
                           wxBITMAP_TYPE_PNG)
        panel = TiledPanel(self, -1, image)

class App(wxApp):
    def OnInit(self):
        wxInitAllImageHandlers()
        frame = Frame(None, -1, "tiled image panel")
        frame.Show(true)
        return true

if __name__ == '__main__':
    app = App()
    app.MainLoop()
    
Now... somehow I missed the discussion of the new packaging scheme and
am not finding the discussion in the archives... someone care to
elaborate how I can use wx.Frame rather than wxFrame?

Cliff

···

On Thu, 2003-06-05 at 17:41, Kimberly Swinth wrote:

I'm a newbie to wxPython and I've been struggling with something for
several days now and was hoping maybe someone out there could help me
with something.

I want to add a background image to one of my frames. I can add a
static image fine. But when I resize the window, the image gets
distorted. So what I really want to do is use a tileable image. In
addition, I want to be able to add other object on top of the image,
and I can't figure out how to do that. I'm a pretty novice
programmer, so there is probably a very simple answer to this
question, but I've been beating my head against the wall for days on
this! The way I'm thinking about this, is I want to do what you do
with a webpage, where you set the background image for the page, and
then you build on top of that. Can this be done in wxPython?

Any help is GREATLY appreciated!

Kim

Should be just:

        cols = w / self.width + 1
        rows = h / self.height + 1

although it didn't hurt anything.

Cliff

···

On Fri, 2003-06-06 at 01:34, Cliff Wells wrote:

       cols = w / self.width + 1 or 1
      rows = h / self.height + 1 or 1

--
When I die, don't bury me alone
                         -- These Immortal Souls

Kimberly Swinth wrote:

I'm a newbie to wxPython and I've been struggling with something for
several days now and was hoping maybe someone out there could help me
with something.

I want to add a background image to one of my frames. I can add a
static image fine. But when I resize the window, the image gets
distorted. So what I really want to do is use a tileable image. In
addition, I want to be able to add other object on top of the image,
and I can't figure out how to do that. I'm a pretty novice
programmer, so there is probably a very simple answer to this
question, but I've been beating my head against the wall for days on
this! The way I'm thinking about this, is I want to do what you do
with a webpage, where you set the background image for the page, and
then you build on top of that. Can this be done in wxPython?

There are a couple examples of doing this in the demo. See the ColourDB.py module and the OnEraseBackground and TileBackground methods.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi Cliff,

Thanks for the help! I had tried looking at the ColourDB example in the demo,
but couldn't figure out what I really needed. With a little "tweaking" I was
able to simplify this even more and get it to work in a Boa application.
Thanks again!

Kim

Quoting Cliff Wells <clifford.wells@attbi.com>:

···

On Thu, 2003-06-05 at 17:41, Kimberly Swinth wrote:
> I'm a newbie to wxPython and I've been struggling with something for
> several days now and was hoping maybe someone out there could help me
> with something.
>
> I want to add a background image to one of my frames. I can add a
> static image fine. But when I resize the window, the image gets
> distorted. So what I really want to do is use a tileable image. In
> addition, I want to be able to add other object on top of the image,
> and I can't figure out how to do that. I'm a pretty novice
> programmer, so there is probably a very simple answer to this
> question, but I've been beating my head against the wall for days on
> this! The way I'm thinking about this, is I want to do what you do
> with a webpage, where you set the background image for the page, and
> then you build on top of that. Can this be done in wxPython?
>
> Any help is GREATLY appreciated!
>
> Kim

I hope this isn't terrible as I'm a few drinks in already (hey, it's
Thursday!). The Draw() method could undoubtedly be optimized.

from wxPython.wx import *

class TiledPanel(wxPanel):
    def __init__(self, parent, id, image):
        wxPanel.__init__(self, parent, id)
        self.image = wxBitmapFromImage(image)
        self.height, self.width = image.GetHeight(), image.GetWidth()
        EVT_PAINT(self, self.OnPaint)

    def OnPaint(self, event):
        self.Draw(wxPaintDC(self))

    def Draw(self, dc):
        h, w = self.GetSize()
        cols = w / self.width + 1 or 1
        rows = h / self.height + 1 or 1
        x = y = 0
        for r in range(rows):
            for c in range(cols):
                dc.DrawBitmap(self.image, y, x, True)
                x += self.width
            y += self.height
            x = 0

class Frame(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self, parent, id, title)
        image = wxImage('/usr/share/pixmaps/gnome-home.png',
                           wxBITMAP_TYPE_PNG)
        panel = TiledPanel(self, -1, image)

class App(wxApp):
    def OnInit(self):
        wxInitAllImageHandlers()
        frame = Frame(None, -1, "tiled image panel")
        frame.Show(true)
        return true

if __name__ == '__main__':
    app = App()
    app.MainLoop()
    
Now... somehow I missed the discussion of the new packaging scheme and
am not finding the discussion in the archives... someone care to
elaborate how I can use wx.Frame rather than wxFrame?

Cliff

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org