dynamically adding & removing widgets, very strange

Hi all!
I need dynamically add and remove some widgets in my application,
it will add finally, but at first, it temporarily *flashes * to the top left corner about 1s.

This square vanishes almost immediately, but it’s clearly visible everytime you add widget.
It’s pretty ugly. I just can’t get rid of it!

Can’t work out why it would appear and then disappear! Is it a programme delay that I can’t get rid of?
Or some error I’m making with my sizers?

Here is my code, thanks!

import wx

···

########################################################################
class MyPanel(wx.Panel):
“”""""

#----------------------------------------------------------------------
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent)
    self.number_of_buttons = 0
    self.frame = parent

    self.mainSizer = wx.BoxSizer(wx.VERTICAL)
    controlSizer = wx.BoxSizer(wx.HORIZONTAL)
    self.widgetSizer = wx.BoxSizer(wx.VERTICAL)

    self.addButton = wx.Button(self, label="Add")
    self.addButton.Bind(wx.EVT_BUTTON, self.onAddWidget)
    controlSizer.Add(self.addButton, 0, wx.CENTER|wx.ALL, 5)

    self.mainSizer.Add(controlSizer, 0, wx.CENTER)
    self.mainSizer.Add(self.widgetSizer, 0, wx.CENTER|wx.ALL, 10)

    self.SetSizer(self.mainSizer)

#----------------------------------------------------------------------
def onAddWidget(self, event):
    """"""
    self.vcbox = wx.FlexGridSizer(3, 2, 5, 10)
    label = wx.StaticText(self, -1, 'verifycode')
    self.vcbox.Add(label, 0, wx.ALIGN_RIGHT| wx.LEFT, 40)
    text = wx.TextCtrl(self, -1,  style = 0)
    self.vcbox.Add(text,1, wx.ALIGN_CENTER_HORIZONTAL| wx.RIGHT ,40)
    tip = wx.StaticText(self, -1, 'some tipsssssss')
    font = wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
    tip.SetFont(font)
    tip.SetForegroundColour('grey')
    imbox = wx.BoxSizer(wx.HORIZONTAL)
    image = wx.StaticBitmap(self, -1, )
    imageTip = wx.StaticText(self, -1, 'some tipsssss')
    imageTip.SetFont(font)
    imageTip.SetForegroundColour('#889DB6')

    imbox.Add(image, 0)
    imbox.Add(imageTip, 0, wx.ALIGN_BOTTOM)

imbox.Layout()

    self.frame.Fit()
    self.vcbox.Add(wx.StaticText(self, -1), 0)
    self.vcbox.Add(tip, 1)
    self.vcbox.Add(wx.StaticText(self, -1), 0)
    self.vcbox.Add(imbox, 1)

    self.widgetSizer.Add(self.vcbox, 0, wx.ALL, 5)
    self.frame.fSizer.Layout()
    self.frame.Fit()

#----------------------------------------------------------------------
def onRemoveWidget(self, event):
    """"""
    pass

########################################################################
class MyFrame(wx.Frame):
“”""""

#----------------------------------------------------------------------
def __init__(self):
    """Constructor"""
    wx.Frame.__init__(self, parent=None, title="Add / Remove Buttons")
    self.fSizer = wx.BoxSizer(wx.VERTICAL)
    panel = MyPanel(self)
    self.fSizer.Add(panel, 1, wx.EXPAND)
    self.SetSizer(self.fSizer)
    self.Fit()
    self.Show()

#----------------------------------------------------------------------
if name == “main”:
app = wx.App(False)
frame = MyFrame()
app.MainLoop()

hello, someone know why?

linzhonghong2010@gmail.com wrote:

hello, someone know why?

You posted this less than 8 hours ago, in the middle of the night for
the US. Have some patience.

···

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

Add this to the beginning of your onAddWidget method:
self.Freeze()
and this to the end:
self.Thaw()
The StaticText control is a little different than other controls.
Most controls create windows that need some initialization before
they can paint, so they don’t draw immediately upon creation.
StaticText doesn’t need that delay, so stuff gets drawn
immediately. Freeze and Thaw are used to put a temporary hold on
all UI activity.

···

wrote:

linzhonghong2010@gmail.com

    Hi all!
    I need dynamically add and remove some widgets in my

application,
it will add finally, but at first, it temporarily *flashes *
to the top left corner about 1s.

    This square vanishes almost immediately, but it's clearly

visible everytime you add widget.

     It's pretty ugly. I just can't get rid of it!



    Can't work out why it would appear and then disappear! Is it a

programme delay that I can’t get rid of?

    Or some error I'm making with my sizers?
-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

sorry, i don’t know the time differences clearly

在 2013年9月11日星期三UTC+8上午12时59分01秒,Tim Roberts写道:

···

linzhong...@gmail.com wrote:

hello, someone know why?

You posted this less than 8 hours ago, in the middle of the night for

the US. Have some patience.


Tim Roberts, ti...@probo.com

Providenza & Boekelheide, Inc.

thank you very much, it works. But another question appear, if i put the function (have some changes) into a dialog,

and add self.Freeze() , self.Thaw(), self is my dialog, it doesn’t work, the statictext flash again,

it doesn’t work for dialog? thanks

在 2013年9月11日星期三UTC+8上午1时05分31秒,Tim Roberts写道:

···

linzhong...@gmail.com wrote:

    Hi all!
    I need dynamically add and remove some widgets in my

application,
it will add finally, but at first, it temporarily *flashes *
to the top left corner about 1s.

    This square vanishes almost immediately, but it's clearly

visible everytime you add widget.

     It's pretty ugly. I just can't get rid of it!



    Can't work out why it would appear and then disappear! Is it a

programme delay that I can’t get rid of?

    Or some error I'm making with my sizers?
Add this to the beginning of your onAddWidget method:

        self.Freeze()

and this to the end:

        self.Thaw()



The StaticText control is a little different than other controls. 

Most controls create windows that need some initialization before
they can paint, so they don’t draw immediately upon creation.
StaticText doesn’t need that delay, so stuff gets drawn
immediately. Freeze and Thaw are used to put a temporary hold on
all UI activity.

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

Hi,

···

On Tuesday, September 10, 2013 8:51:16 PM UTC-5, linzhong...@gmail.com wrote:

thank you very much, it works. But another question appear, if i put the function (have some changes) into a dialog,

and add self.Freeze() , self.Thaw(), self is my dialog, it doesn’t work, the statictext flash again,

it doesn’t work for dialog? thanks

Maybe not. But you can use a panel in a dialog just like you do with a frame. Create the panel and make it the only child widget of the dialog and the other widgets can be children of the panel. Then it should work again.

  • Mike