problems showing button and image at the same time

Hi-

I've just started playing with wxpython (yesterday) and overall I'm damn impressed (its a billion times better than tkinter) but I can't get an image and button to show up at the same time.

any ideas whats wrong with this code?

import wx
import time
import os

app = wx.App(redirect=False)

class Frame(wx.Frame):

  def __init__(self, image):
    wx.Frame.__init__(self, None, title="myGuts", size=(350,200), style=wx.DEFAULT_FRAME_STYLE)
    temp = image.ConvertToBitmap()
    size = 1125, 675
        
    wx.Button(panel, -1, "Button1", pos=(300,500))

    self.bmp = wx.StaticBitmap(self, bitmap=temp)
    self.SetClientSize(size)

class MyGuts(wx.App):
  
  def OnInit(self):
    
    image = wx.Image('moo.png', wx.BITMAP_TYPE_PNG)
    w, h = image.GetWidth(), image.GetHeight()
    scaledImage = image.Scale(w*.2, h*.2)
    
    self.frame = Frame(scaledImage)
    
    self.frame.Show()
    self.frame.CentreOnScreen()
    self.frame.SetBackgroundColour('black')
    return True

class Screenshooter():
  
  def __init__(self):
    currentTimeFile = strftime("%d%m%H%M%S%Y")
    os.system('screencapture -x' + str(currentTimeFile) + '.png')
    image1 = Image.open("moo.png")
    return currentTimeFile
    
def main():
  app = MyGuts()
  app.MainLoop()
  
if __name__ == '__main__':
  main()

thanks

Hi Dom,

Hi-

I've just started playing with wxpython (yesterday) and overall I'm damn impressed (its a billion times better than tkinter) but I can't get an image and button to show up at the same time.

Welcome to the world of wxPython!

any ideas whats wrong with this code?

import wx
import time
import os

app = wx.App(redirect=False)

class Frame(wx.Frame):

    def __init__(self, image):
        wx.Frame.__init__(self, None, title="myGuts", size=(350,200), style=wx.DEFAULT_FRAME_STYLE)
        temp = image.ConvertToBitmap()
        size = 1125, 675
                       wx.Button(panel, -1, "Button1", pos=(300,500))

        self.bmp = wx.StaticBitmap(self, bitmap=temp)
        self.SetClientSize(size)

class MyGuts(wx.App):
        def OnInit(self):
       
        w, h = image.GetWidth(), image.GetHeight()
        scaledImage = image.Scale(w*.2, h*.2)
       
               self.frame.Show()
        self.frame.CentreOnScreen()
        self.frame.SetBackgroundColour('black')
        return True

class Screenshooter():
        def __init__(self):
        currentTimeFile = strftime("%d%m%H%M%S%Y")
        os.system('screencapture -x' + str(currentTimeFile) + '.png')
        image1 = Image.open("moo.png")
        return currentTimeFile
       def main():
    app = MyGuts()
    app.MainLoop()
    
    main()

thanks

It looks like you stuck the button on a non-existent panel (which throws an error on my machine) and set the StaticBitmap's parent as the frame. If you make the parent of BOTH the button and the bitmap as either the panel or the frame, it should work. Note that the panel is needed to look "right" on Windows.

Just an FYI, here's the general heirarchy to use:

wx.App --> wx.Frame --> wx.Panel --> some kind of sizer --> widgets (like text controls, grids, etc)

Anyway, here's a snippet of what I did:

<code>

panel = wx.Panel(self, wx.ID_ANY) # added a panel
wx.Button(panel, -1, "Button1", pos=(300,500))

# set this widget's parent to the panel too
self.bmp = wx.StaticBitmap(panel, bitmap=temp)

</code>

Hope that helps!

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org