why crash my sample?

import wx

class Panel(object):
    def __init__(self):
        object.__init__(self)

        self._size = None
        self._caption = ""

    def GetSize(self, size):
        self._size = size

        return self

    def Caption(self, caption):
        self._caption = caption

        return self

class cWindow(wx.Window):
    def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.SIMPLE_BORDER, name="cWindow"):
        wx.Window.__init__(self, parent, id, pos, size, style, name)

def GetCheckedBitmap():
    return wx.BitmapFromImage(GetCheckedImage())

def GetCheckedImage():
    return wx.Image("checked.ico", wx.BITMAP_TYPE_ICO)

def GetNotCheckedBitmap():
    return wx.BitmapFromImage(GetNotCheckedImage())

def GetNotCheckedImage():
    return wx.Image("notchecked.ico", wx.BITMAP_TYPE_ICO)

def GrayOut(anImage):
    factor = 0.7
    if anImage.HasMask():
        maskColor = (anImage.GetMaskRed(), anImage.GetMaskGreen(),
anImage.GetMaskBlue())
    else:
        maskColor = None

    data = map(ord, list(anImage.GetData()))

    for i in range(0, len(data), 3):
        pixel = (data[i], data[i+1], data[i+2])
        pixel = MakeGray(pixel, factor, maskColor)

        for x in range(3):
            data[i+x] = pixel[x]

    anImage.SetData(''.join(map(chr, data)))

    return anImage.ConvertToBitmap()

def MakeGray((r,g,b), factor, maskColor):
    if(r,g,b)!=maskColor:
        return map(lambda x: int((230 - x) * factor)+ x , (r,g,b))
    else:
        return (r,g,b)

class cItem(wx.PyControl):
    def __init__(self, parent, id=wx.ID_ANY, label="",
pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.NO_BORDER,
validator=wx.DefaultValidator, name="cItem"):
        wx.PyControl.__init__(self, parent, id, pos, size, style,
validator, name)

        self._isHover = False
        self._isSelect = False
        self._hasFocus = False
        self._checked = False
        self.bitmaps = {"CheckEnable": GetCheckedBitmap(),
                        "UnCheckedEnable": GetNotCheckedBitmap(),
                        "CheckedDisable": GrayOut(GetCheckedImage()),
                        "UnCheckedDisable":
GrayOut(GetNotCheckedImage())}
        self.SetLabel(label)
        self._spacing = 3

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

        self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseClick)
        if wx.Platform == '__WXMSW__':
            self.Bind(wx.EVT_LEFT_DCLICK, self.OnMouseClick)

        self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
        self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
        self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)

    def OnPaint(self, event):
        dc = wx.BufferedPaintDC(self)
        self.Draw(dc)

    def SetLabel(self, label):
        wx.PyControl.SetLabel(self, label)
        self.Refresh()

    #def GetLabel()

    def SetSpacing(self, spacing):
        self._spacing = spacing
        self.Refresh()

    def GetSpacing(self):
        return self._spacing

    def HasFocus(self):
        return self._hasFocus

    def Draw(self, dc):
        width, height = self.GetClientSize()

        if not width or not height:
            return

        backcolor = self.GetBackgroundColour()
        backbrush = wx.Brush(backcolor, wx.SOLID)

        dc.SetBackground(backbrush)
        dc.Clear()

        if self.IsEnabled():
            dc.SetTextForeground(self.GetForegroundColour())
        else:

dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT))

        dc.SetFont(self.GetFont())

        label = self.GetLabel()
        bitmap = self.GetBitmap()
        spacing = self.GetSpacing()

        textW, textH = dc.GetTextExtent(label)
        bitmapW, bitmapH = bitmap.GetWidth(), bitmap.GetHeight()

        bmpXpos = 0
        bmpYpos = (height - bitmapH)/2

        textXpos = bitmapW + spacing
        textYpos = (height - textH)/2

        dc.DrawBitmap(bitmap, bmpXpos, bmpYpos, True)
        dc.DrawText(label, textXpos, textYpos)

        if self.HasFocus():
            dc.SetBrush(wx.TRANSPARENT_BRUSH)
            dc.SetPen(wx.BLACK_PEN)
            dc.DrawRectangle(textXpos, textYpos, textW, textH)

    def OnEraseBackground(self, event):
        pass

    def OnMouseClick(self, event):
        if not self.IsEnabled():
            return

        self.SendMenuEvent()
        event.Skip()

    def Enable(self, enable=True):
        wx.PyControl.SetBackgroundColour(self, enable)
        self.Refresh()

    def SendMenuEvent(self):
        if self.IsSelected():
            self.isHover = False

            selectEvent =
wx.ComandEvent( wx.wxEVT_COMMAND_MENU_SELECTED, GetId())

            selectEvent.SetInt(0)

        else:

            self.isHover = True

            selectEvent =
wx.CommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED, GetId())
            selectEvent.SetInt(1)

        selectEvent.SetEventObject(self)
        self.GetEventHandler().ProcessEvent(selectEvent)

        self.Refres()

    def OnKeyUp(self, event):
        if event.GetKeyCode() == wx.WXK_ENTER:
            self.SendMenuEvent()
            event.Skip()
            return

        event.Skip()

    def OnSetFocus(self, event):
        self._hasFocus = True
        self.Refresh()

    def OnKillFocus(self, event):
        self._hasFocus = False

        self.Refresh()

    def AcceptxFocusFromKeyboard(self):
        return True

    def AcceptsFocus(self):
        return False

    def GetDefaultAttributes(self):
        pass

        #return wx.MenuItem.GetClassDefaultAttributes()

    def ShowIdInheritColours(self):
        return True

    def DoGetBestSize(self):

        label = self.GetLabel()
        font = self.GetFont()

        bitmap = self.GetBitmap()

        if not font:
            font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)

        dc = wx.ClientDC(self)
        dc.SetFont()

    def GetValue(self):
        return self._isSelect

    def IsChecked(self):
        return self._checked

    def SetValue(self, state):
        self._checked = state

        self.Refresh()

    def GetBitmap(self):

        if self.IsEnabled():
            if self.IsChecked():
                return self.bitmaps["CheckedEnable"]
            else:
                return self.bitmaps["UnCheckedEnable"]
        else:
            if self.IsChecked():
                return self.bitmaps["ChecedDisable"]
            else:
                return self.bitmaps["UnCheckedDisable"]

class Frame(wx.MiniFrame):
    def __init__(self, parent, id, title, pos, size, style, name):
        style = wx.DEFAULT_FRAME_STYLE
        title = ""
        wx.MiniFrame.__init__(self, parent, id, title, pos, size,
style, name)

        self._title = ""

    def GetTitle(self, title):
        self._title = title
        return self

class MainFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        windo = Frame(self, -1, "Tool", wx.DefaultPosition,
wx.Size(200, 200), wx.DEFAULT_FRAME_STYLE, "Window")

        menubar = wx.MenuBar()
        menu = wx.Menu()
        menu.Append(10, "Window", "")
        menubar.Append(menu, "Tool")
        self.SetMenuBar(menubar)

        panel = wx.Panel(self, -1)
        item = cItem(panel, -1, "Menu")

app = wx.App()
MainFrame(None, -1, "Test-10").Show()
app.MainLoop()

This is too much code for me to want to look through:

http://wiki.wxpython.org/MakingSampleApps

However, I do see, right at the top:

class Panel(object):
     def __init__(self):
         object.__init__(self)

The usual idiom is:

class Panel(object):
      def __init__(self):
          wx.Panel.__init__(self)

That is, you want to initialize the superclass of your Panel.

And the wx.Panel init needs at least one argument -- parent:

class Panel(object):
      def __init__(self, parent):
          wx.Panel.__init__(self, parent)

but usually you might want to pass in more:

class Panel(object):
      def __init__(self, *args, **kwargs):
          wx.Panel.__init__(self, *args, **kwargs)

If you really did mean to initialize the object being passed in, that is a pretty odd architecture.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

that could be but i'm not calling that class that i've use is the
cItem class

when you post code for us to review, you really only want to include the minimum amouth to demonstate the problem -- don't post classed you aren't using.

If you want folks to help you, you need to make it easy to do so.

-Chris

···

On 3/20/11 9:13 PM, iozk_Live wrote:

that could be but i'm not calling that class that i've use is the
cItem class

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

class Panel(object):
def init(self):
object.init(self)

This is obviously not the right way to do it. The right way is…

class Panel(wx.Panel):

def __init__(self):
    wx.Panel.__init__(self)

iozk_Live wrote:

...long application deleted...

I can get you started on working around this, although I don't quite
know the root cause.

It is crashing in the call to wx.BufferedPaintDC, trying to grab a
reference to a null object. If I change it to wx.PaintDC, it still
crashes there with the same symptom. The null object appears to be the
background color, the foreground color, or the font. I added the
following three lines just before the call to SetLabel, and it allows
your application to run:

        self.SetForegroundColour( '#000000')
        self.SetBackgroundColour( '#ffffff')
        self.SetFont( wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT))

There are other problems here, however.

    def SetLabel(self, label):
        wx.PyControl.SetLabel(self, label)
        self.Refresh()

This call serves no purpose. You might as well remove it.

    def Enable(self, enable=True):
        wx.PyControl.SetBackgroundColour(self, enable)
        self.Refresh()

That's wrong. SetBackgroundColor expects a color, not a boolean value.

    def DoGetBestSize(self):

        label = self.GetLabel()
        font = self.GetFont()

        bitmap = self.GetBitmap()

        if not font:
            font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)

        dc = wx.ClientDC(self)
        dc.SetFont()

That's wrong in several ways. The dc.SetFont call needs a font, but you
shouldn't need a DC at all here. The only reason it didn't crash here
first is because you removed the "self.SetInitialSize" call that was in
the original sample.

By the way, you could have narrowed this down the way I did, by
sprinkling "print" statements every few lines to get a trace, and of
course passing a zero to the App constructor so you can see the trace:

   app = wx.App(0)

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Only if he actually wanted his Panel class to be a wx.Panel. :wink:

···

On 3/21/11 1:40 AM, Bo�tjan Mejak wrote:

    class Panel(object):
         def __init__(self):
             object.__init__(self)

This is obviously not the right way to do it. The right way is...

class Panel(wx.Panel):

     def __init__(self):
         wx.Panel.__init__(self)

--
Robin Dunn
Software Craftsman

My point exactly.

You would get better/quicker help if you:

- only posted the code which is needed to reproduce the problem
- attach the code - putting it into the message messes up the indentation ....

http://wiki.wxpython.org/MakingSampleApps

Do you get a hard crash, or an exception, if the later include the exception and it might also be helpful to provide the version numbers (Python and wxPython and OS in your case).

I just tried to copy/paste it but the code is messed up and not runnable, so I don't go any further with it.

Werner

···

On 21/03/2011 05:13, iozk_Live wrote:

that could be but i'm not calling that class that i've use is the
cItem class