wxPython: Default Frame button?

Discussion in 'Python' started by Miki Tebeka, Aug 3, 2003.

  1. Miki Tebeka

    Miki Tebeka Guest

    Hello All,

    I have a frame that contains a panel and several buttons.
    I'd like to make one of the button the default button but
    self.SetDefaultItem(btn) or btn.SetFocus() don't work. The item in
    focus is a text control inside the panel.

    Any Ideas? (see short example below)

    Thanks.
    Miki

    --- btn.py ---
    import wx

    class P(wx.Panel):
    def __init__(self, parent, id=-1):
    wx.Panel.__init__(self, parent, id)
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(wx.TextCtrl(self, -1, size=(250, -1), value="XXX"))

    self.SetAutoLayout(True)
    self.SetSizer(sizer)
    sizer.Fit(self)

    class F(wx.Frame):
    def __init__(self):
    wx.Frame.__init__(self, None, -1, "Test Frame")
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(P(self), 0, wx.EXPAND)
    b = wx.Button(self, wx.NewId(), "Quit")
    wx.EVT_BUTTON(self, b.GetId(), self.on_quit)
    sizer.Add(b, 0, wx.EXPAND)

    self.SetDefaultItem(b)
    b.SetFocus()

    self.SetAutoLayout(True)
    self.SetSizer(sizer)
    sizer.Fit(self)


    def on_quit(self, e):
    self.Close(True)


    def main():
    app = wx.PySimpleApp()
    f = F()
    f.Show(True)
    app.MainLoop()

    if __name__ == "__main__":
    main()

    --- btn.py ---
     
    Miki Tebeka, Aug 3, 2003
    #1
    1. Advertising

  2. Miki Tebeka

    Cliff Wells Guest

    On Mon, 2003-08-04 at 06:22, Miki Tebeka wrote:
    > Hello Cliff,
    >
    > > In general, the only child of a frame should be a panel or some other
    > > container (like a splitter). Frames should generally only have one
    > > child. Make the button a child of the panel rather than a sibling.

    > 10x. Works like a charm.
    >
    > >class F(wx.Frame):
    > > def __init__(self):
    > > wx.Frame.__init__(self, None, -1, "Test Frame")
    > > panel = P(self)
    > > self.Fit()

    >
    > Is there a default frame that does the above?


    No. However, you can combine the frame and panel code into a single
    class if you like:

    import wx

    class F(wx.Frame):
    def __init__(self):
    wx.Frame.__init__(self, None, -1, "Test Frame")
    panel = wx.Panel(self, -1)
    sizer = wx.BoxSizer(wx.VERTICAL)

    b = wx.Button(panel, -1, "Quit")
    wx.EVT_BUTTON(panel, b.GetId(), self.on_quit)

    sizer.AddMany([
    (wx.TextCtrl(panel, -1, size = (250, -1), value = "XXX")),
    (b, 0, wx.EXPAND),
    ])

    panel.SetDefaultItem(b)
    b.SetFocus()

    panel.SetAutoLayout(True)
    panel.SetSizer(sizer)
    sizer.Fit(panel)

    self.Fit()

    def on_quit(self, e):
    self.Close(True)


    def main():
    app = wx.PySimpleApp()
    f = F()
    f.Show(True)
    app.MainLoop()


    if __name__ == "__main__":
    main()



    Alternatively, you can dispense with creating a custom class for the
    frame (which is perhaps a bit closer to what you are asking):


    import wx

    class P(wx.Panel):
    def __init__(self, parent, id = -1):
    wx.Panel.__init__(self, parent, id)
    sizer = wx.BoxSizer(wx.VERTICAL)

    b = wx.Button(self, -1, "Quit")
    wx.EVT_BUTTON(self, b.GetId(), lambda evt: parent.Close(True))

    sizer.AddMany([
    (wx.TextCtrl(self, -1, size = (250, -1), value = "XXX")),
    (b, 0, wx.EXPAND),
    ])

    self.SetDefaultItem(b)
    b.SetFocus()

    self.SetAutoLayout(True)
    self.SetSizer(sizer)
    sizer.Fit(self)


    def main():
    app = wx.PySimpleApp()
    f = wx.Frame(None, -1, "Test Frame")
    P(f)
    f.Fit()
    f.Show(True)
    app.MainLoop()


    if __name__ == "__main__":
    main()



    Personally, I prefer to keep objects discrete, especially for panels
    which might at some point get moved to some other container (say you
    decide to put a notebook or splitter in the frame), but if you're not
    concerned about that then these are both valid approaches.

    Regards,

    --
    Cliff Wells, Software Engineer
    Logiplex Corporation (www.logiplex.net)
    (503) 978-6726 (800) 735-0555
     
    Cliff Wells, Aug 4, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ABC
    Replies:
    0
    Views:
    596
  2. Anand
    Replies:
    1
    Views:
    357
    Cliff Wells
    Jul 23, 2003
  3. ulysses
    Replies:
    4
    Views:
    778
    Werner Schiendl
    Oct 22, 2003
  4. Marcpp
    Replies:
    4
    Views:
    530
    Roel Schroeven
    Jul 5, 2007
  5. «÷¥¬¤ý
    Replies:
    0
    Views:
    455
    «÷¥¬¤ý
    Sep 1, 2009
Loading...

Share This Page