[wxPython-users] wx.Panel question

Christopher Barker Chris.Barker at noaa.gov
Wed Sep 19 21:20:47 PDT 2007


Regan Tackett wrote:


> it states that many programmers create a single panel on the 
> frame to hold all of the contents of that frame.

yup, wx.Panel has a few features designed for putting widgets on it.

> However, if I run this code and resize the frame 
> (window), the panel remains the same size.

This was answered for you.

>  Also, am I 
> suppose to but each widget in its own panel,

not at all -- the whole point of Panel is a container for widgets. You 
may want to group widgets that work together on on panel though.

A couple style comments:

Now is a good time to read:

  http://wiki.wxpython.org/wxPython_Style_Guide

It's help you develop "good" style.

> Here’s the code:

One comment -- there are a lot of parallel hierarchies in wxPython:

  - wx.Window parent child relationships
  - Nested wx.Sizers
  - Python classes.

While not necessary, I like to keep the python classes in sync with the 
Windows -- in your case, you have widgets on a panel, which is on a 
Frame -- I think that's too deep, and it was the source of your error. 
I'd make a custom Panel instead (untested) (note other style changes as 
well):

#/usr/bin/env python

import wx

class MyGUI(wx.App):
      def OnInit(self):
          self.frame = Frame("MyGUI", (400, 300))
          self.frame.CenterOnScreen()
          self.frame.Show()
          self.SetTopWindow(self.frame)
          return True

class MainPanel(wx.Panel):
      def __init__(self, *args, **kwargs):
          # I use this construction so that my derived panels act like 

          #   other panels.
          wx.Frame.__init__(self, *args, **kwargs)
          button1 = wx.Button(self, label="Open")
          # don't set a size -- that's what sizers are for!
          button2 = wx.Button(self, label="Close")
          text = wx.TextCtrl(self.mainPanel, style=wx.TE_MULTILINE)

          box = wx.BoxSizer(wx.HORIZONTAL)
          box.Add(button1)
          box.Add(button2)
          box.Add(text, wx.EXPAND)
          self.SetSizer(box)
          self.Fit()


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

          self.mainPanel = MainPanel(self)
          self.Fit()
          ## now you'd have menu handlers and the like here.


def main():

     app = MyGUI(False)
     app.MainLoop()

if __name__ == "__main__":
      main()


-- 
Christopher Barker, Ph.D.
Oceanographer

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




More information about the wxpython-users mailing list