Problem adding buttons to notebook page

I'm experiencing some strange behavior when I add a page to a
wx.lib.agw.flatnotebook. Specifically when I create a new page and add
lots of buttons to it, the buttons draw on the frame before they get
added to the page.

Also my binding to trigger wx.EVT_RIGHT_UP on a button doesn't seem to
working.

I'd appreciate some help. =)

(I tried to rip out as much of the non-relevant code as possible, but
I might have missed some)

<code>
import wx

from wx.lib.agw.flatnotebook import FlatNotebook
from wx.lib.scrolledpanel import ScrolledPanel

from wx.lib.buttons import GenButton

[ID_Self, ID_NewPalette, ID_ImportSaveState, ID_ImportFromThisFile,
ID_Randomize, ID_MenuBar, ID_MENU_EDIT_RENAME_PAGE] = [wx.NewId() for
num in range(7)]

FRAME_SIZE = wx.Size(275, 325)

···

#
-----------------------------------------------------------------------------
class PaletteFrame(wx.MiniFrame):

    def __init__(self, prnt):
        wx.MiniFrame.__init__(self, prnt, id=ID_Self, title='Color
Palette',
                              size=FRAME_SIZE,
style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_TOOL_WINDOW |
wx.FRAME_NO_TASKBAR )

        self.book = ColoringBook(self)
  self.__createPaletteMenu()

    def __createPaletteMenu(self):
        def doBind(item, handler, updateUI=None):
            self.Bind(wx.EVT_MENU, handler, item)
            if updateUI is not None:
                self.Bind(wx.EVT_UPDATE_UI, updateUI, item)

        menubar = wx.MenuBar()

        paletteMenu = wx.Menu()
        doBind(paletteMenu.Append(ID_NewPalette, "New", "Open a
file"), self.OnNew)

        menubar.Append(paletteMenu, "Palette")
        self.SetMenuBar(menubar)

    def OnNew(self, evt):
  numButtons = 256
  page = ColoringPage(self, numButtons)
  self.book.AddPage(page)
  self.Refresh()

#
-----------------------------------------------------------------------------
class ColoringBook(wx.lib.agw.flatnotebook.FlatNotebook):
    def __init__(self, prnt):
        wx.lib.agw.flatnotebook.FlatNotebook.__init__(self, prnt)
  page = ColoringPage(self)
  self.AddPage(page)

  # How do we do this in the constructor?
  style = self.GetWindowStyleFlag()
  style |= wx.lib.agw.flatnotebook.FNB_X_ON_TAB
  style |= wx.lib.agw.flatnotebook.FNB_MOUSE_MIDDLE_CLOSES_TABS
  style |= wx.lib.agw.flatnotebook.FNB_NO_X_BUTTON

        self.SetWindowStyleFlag(style)

    def AddPage(self, page):
        wx.lib.agw.flatnotebook.FlatNotebook.AddPage(self, page,
'test')

#
-----------------------------------------------------------------------------

class ColoringPage(ScrolledPanel):

    def __init__(self, prnt, size=256, title='Default'):
  ScrolledPanel.__init__(self, prnt)
  self.sizer = wx.GridSizer(16,16)
  self.SetSizer(self.sizer)
  self.SetAutoLayout(1)
  self.SetupScrolling()

  self.size = size
  self.title = title

  for color in xrange(size):
      self.__createButton()

    def __createButton(self):
  colorBox = ColoringButton(prnt=self)
  self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp, colorBox)
  self.sizer.Add(colorBox, 0, wx.ALL, 1)

    def OnRightUp(self, evt):
  print 'On right Up'
  evt.skip()

#
-----------------------------------------------------------------------------
class ColoringButton(wx.lib.buttons.GenButton):
    def __init__(self, prnt):
  wx.lib.buttons.GenButton.__init__(self, parent = prnt, id=-1, size =
wx.Size(12, 12),
                                    style =
wx.NO_FULL_REPAINT_ON_RESIZE | wx.SIMPLE_BORDER)

#
-----------------------------------------------------------------------------
if __name__ == '__main__':
    class MyApp(wx.App):
  def OnInit(self):
      frame = PaletteFrame(None)
      frame.Show(True)
      self.SetTopWindow(frame)
      return True

    app = MyApp(False)
    app.MainLoop()
</code>

Hi,

I'm experiencing some strange behavior when I add a page to a
wx.lib.agw.flatnotebook. Specifically when I create a new page and add
lots of buttons to it, the buttons draw on the frame before they get
added to the page.

I don't see this issue on my machine. You may want to try wrapping the page creation and its addition to the notebook with Freeze/Thaw to prevent the screen from repainting until after the page has been added.

self.Freeze()
makepage()
addpage()
self.Thaw()

Also my binding to trigger wx.EVT_RIGHT_UP on a button doesn't seem to
working.

Because your binding EVT_RIGHT_UP to the panel and not to the button.

In the future please attach a file instead of doing this, otherwise all the formatting gets eaten up like this and your sample becomes un-runnable without a bunch of reformatting.

class ColoringBook(wx.lib.agw.flatnotebook.FlatNotebook):
   def __init__(self, prnt):
       wx.lib.agw.flatnotebook.FlatNotebook.__init__(self, prnt)
  page = ColoringPage(self)
  self.AddPage(page)

  # How do we do this in the constructor?
  style = self.GetWindowStyleFlag()
  style |= wx.lib.agw.flatnotebook.FNB_X_ON_TAB
  style |= wx.lib.agw.flatnotebook.FNB_MOUSE_MIDDLE_CLOSES_TABS
  style |= wx.lib.agw.flatnotebook.FNB_NO_X_BUTTON

       self.SetWindowStyleFlag(style)

Use the ctors 'style' parameter.

   def __createButton(self):
  colorBox = ColoringButton(prnt=self)
  self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp, colorBox)
  self.sizer.Add(colorBox, 0, wx.ALL, 1)

Should be colorBox.Bind(...)

Cody

···

On Jan 15, 2010, at 6:18 PM, beissemj wrote:

Cody,

Thank you for the response, and for reformatting my code to test it. I
was unaware that I could attach files to posts, as I usually view the
forums directly in the google groups. In the future I will be sure to
attach sample code in a file.

Your correction of colorBox.Bind(...) does indeed fix my problem. I am
still seeing the same behavior on my machine even with the Freeze and
Thaw, but that does seem to be the correct fix, so I will need to
investigate more.