wxPython, dynamically modify window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant

    #1

    wxPython, dynamically modify window

    Hi, I am looking for a tip. I have a panel and a checkbox. When I
    check the checkbox, I would like to add buttons to the panel
    (dynamically). When the checkbox is unchecked, the buttons should not
    appear (be deleted)---all the while, the window should resize if necessary.

    If you have a simpler example, that is fine. I just need a hint as to
    how you dynamically change the widgets and their layouts.

    Looking at the wx demos, there is something close wx.lib.expando, but
    this is just modifying a particular widget.

    Thanks.
  • cmcp

    #2
    Re: wxPython, dynamically modify window


    Grant wrote:
    Hi, I am looking for a tip. I have a panel and a checkbox. When I
    check the checkbox, I would like to add buttons to the panel
    (dynamically). When the checkbox is unchecked, the buttons should not
    appear (be deleted)---all the while, the window should resize if necessary.
    >
    Here is one way that seems to work (wxPython 2.7, Python 2.5, Mac OS X
    10.4.8). It uses a sizer's Hide() and Show() methods to control
    visibility of a child sizer containing the buttons. To resize the frame
    containing the checkbox and buttons, ensure the frame has a sizer and
    use the Fit() method after Hiding and Showing the buttons.
    HTH,
    -- CMcP

    import wx

    class AppFrame(wx.Fra me):
    def __init__(self, parent, title):
    wx.Frame.__init __(self, parent, -1, title)
    panel = wx.Panel(self, -1)
    panel_sizer = wx.BoxSizer(wx. VERTICAL)
    panel.SetSizer( panel_sizer)

    cb = wx.CheckBox(pan el, -1, 'Need some buttons')
    cb.SetValue(Fal se)
    self.Bind(wx.EV T_CHECKBOX, self.EvtCheckBo x, cb)

    button_sizer = wx.BoxSizer(wx. HORIZONTAL)
    b1 = wx.Button(panel , -1, 'Button 1')
    b2 = wx.Button(panel , -1, 'Button 2')
    button_sizer.Ad d(b1, 0, wx.ALL, 5)
    button_sizer.Ad d(b2, 0, wx.ALL, 5)

    panel_sizer.Add (cb, 0, wx.ALL, 5)
    panel_sizer.Add (button_sizer)

    panel_sizer.Hid e(button_sizer, recursive=True)

    frame_sizer = wx.BoxSizer()
    frame_sizer.Add (panel, 1, wx.EXPAND)

    self.SetSizer(f rame_sizer)
    self.panel_size r = panel_sizer
    self.button_siz er = button_sizer

    self.Fit()

    def EvtCheckBox(sel f, event):
    cb = event.GetEventO bject()
    if cb.GetValue() == True:
    self.panel_size r.Show(self.but ton_sizer, recursive=True)
    else:
    self.panel_size r.Hide(self.but ton_sizer, recursive=True)
    self.Fit()

    class App(wx.App):
    def OnInit(self):
    frame = AppFrame(None, 'Hide/Show Example')
    self.SetTopWind ow(frame)
    frame.Show()
    return True

    if __name__ == '__main__':
    app = App()
    app.MainLoop()

    Comment

    Working...