sizer usage

Never done this mailing list thing before,
hope this is the right way to do it…

I am having trouble getting started
with wxPython, I have been reading tutorials and playing with demos and
still find myself unable to do basic things.

I found I was able to take one demo
and change the widgets on it to start approximating a calculator interface,
however as soon as I tried to add code (not even my own, the buttons were
from a different tutorial), it started placing all my elements in the upper
right corner (stacked). I was hoping someone could enlighten me as to what
I am doing wrong with these sizers.

The code is:

import wx

class MyForm(wx.Frame):

def init(self):

  wx.Frame.__init__(self,

None, wx.ID_ANY, ‘My Form’)

  # Add a panel so

it looks the correct on all platforms

  self.panel = wx.Panel(self,

wx.ID_ANY)

  font = wx.Font(12,

wx.SWISS, wx.NORMAL, wx.BOLD)

  display = wx.TextCtrl(self.panel,

wx.ID_ANY, ‘0’, style=wx.TE_READONLY | wx.TE_RIGHT)

  display.SetFont(font)

 

  # Create the sizers

  topSizer  

= wx.BoxSizer(wx.VERTICAL)

  ButtonSizer  

= wx.GridSizer(4,4,3,3)

  # This is the code

that I added; it is from a different demo

  ButtonSizer.AddMany(

[ (wx.Button(self, -1, ‘Cls’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘Bck’), 0, wx.EXPAND),

(wx.StaticText(self, -1, ‘’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘Close’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘7’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘8’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘9’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘/’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘4’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘5’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘6’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘*’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘1’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘2’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘3’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘-’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘0’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘.’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘=’), 0, wx.EXPAND),

(wx.Button(self, -1, ‘+’), 0, wx.EXPAND) ])

  # Add sub-sizers

to topSizer

  topSizer.Add(display,

0, wx.EXPAND|wx.ALL)

  topSizer.Add(ButtonSizer,

0, wx.ALL|wx.EXPAND, 5)

  self.panel.SetSizer(topSizer)

  # SetSizeHints(minW,

minH, maxW, maxH)

  self.SetSizeHints(250,200,700,300)

  topSizer.Fit(self)

Run the program

if name == ‘main’:

app = wx.PySimpleApp()

frame = MyForm().Show()

app.MainLoop()

It should put the display at the top
filling the sizer (worked before I added the other code) and the buttons
down below it (resizeable). I am working under windows, so I am trying
to put everything in a panel so it looks good.

Thank you all for your help.

-Brian

it started placing all my elements in the upper right corner
(stacked). I was hoping someone could enlighten me as to what I am
doing wrong with these sizers.

      self.panel = wx.Panel(self, wx.ID_ANY)
       
      ButtonSizer = wx.GridSizer(4,4,3,3)

            (wx.Button(self, -1, 'Close'), 0, wx.EXPAND),

You need to have the parent of the buttons be self.panel

i.e.

(wx.Button(self.panel, -1, 'Close'), 0, wx.EXPAND),

Mark

I think it is a "parenting" problem.

The buttons should have as their parent 'self.panel'

As written , the buttons are parented to the frame and the sizer the
buttons are linked to is attached to the panel.

Geoff.

Thanks, I actually noticed that just
a few minutes after posting. Seems I was running low on caffene yesterday
when I was trying to figure this one out.

-Brian

Mark Erbaugh mark@microenh.com

Sent by: wxpython-users-bounces@lists.wxwidgets.org

No Phone Info Available
06/24/2008 09:18 AM

Please respond to

mark@microenh.com; Please respond to

wxpython-users@lists.wxwidgets.org

To

wxpython-users@lists.wxwidgets.org
cc

Subject

Re: [wxpython-users] sizer usage

`

it started placing all my elements in the upper right corner

(stacked). I was hoping someone could enlighten me as to what I am

doing wrong with these sizers.

  self.panel = wx.Panel(self, wx.ID_ANY)
  ButtonSizer    = wx.GridSizer(4,4,3,3)
        (wx.Button(self, -1, 'Close'),

0, wx.EXPAND),

You need to have the parent of the buttons be self.panel

i.e.

(wx.Button(self.panel, -1, ‘Close’), 0, wx.EXPAND),

Mark

···

wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

`

It took me a while, but then I realized that those widgets you were adding to the GridSizer are getting the frame as their parent rather than the panel. Instead of passing them "self", you should pass then "self.panel" instead. For example:

This wx.Button(self, -1, '+'), 0, wx.EXPAND) should be this wx.Button(self.panel, -1, '+'), 0, wx.EXPAND)

Then it works (at least it does on Windows XP).

···

brian.d.fett@seagate.com wrote:

Never done this mailing list thing before, hope this is the right way to do it...

I am having trouble getting started with wxPython, I have been reading tutorials and playing with demos and still find myself unable to do basic things.

I found I was able to take one demo and change the widgets on it to start approximating a calculator interface, however as soon as I tried to add code (not even my own, the buttons were from a different tutorial), it started placing all my elements in the upper right corner (stacked). I was hoping someone could enlighten me as to what I am doing wrong with these sizers.

The code is:
import wx
class MyForm(wx.Frame):

   def __init__(self):
      wx.Frame.__init__(self, None, wx.ID_ANY, 'My Form')

      # Add a panel so it looks the correct on all platforms
      self.panel = wx.Panel(self, wx.ID_ANY)
           font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)
      display = wx.TextCtrl(self.panel, wx.ID_ANY, '0', style=wx.TE_READONLY | wx.TE_RIGHT)
      display.SetFont(font)

           # Create the sizers
      topSizer = wx.BoxSizer(wx.VERTICAL)
      ButtonSizer = wx.GridSizer(4,4,3,3)

      # This is the code that I added; it is from a different demo
      ButtonSizer.AddMany( [ (wx.Button(self, -1, 'Cls'), 0, wx.EXPAND),
            (wx.Button(self, -1, 'Bck'), 0, wx.EXPAND),
            (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
            (wx.Button(self, -1, 'Close'), 0, wx.EXPAND),
            (wx.Button(self, -1, '7'), 0, wx.EXPAND),
            (wx.Button(self, -1, '8'), 0, wx.EXPAND),
            (wx.Button(self, -1, '9'), 0, wx.EXPAND),
            (wx.Button(self, -1, '/'), 0, wx.EXPAND),
            (wx.Button(self, -1, '4'), 0, wx.EXPAND),
            (wx.Button(self, -1, '5'), 0, wx.EXPAND),
            (wx.Button(self, -1, '6'), 0, wx.EXPAND),
            (wx.Button(self, -1, '*'), 0, wx.EXPAND),
            (wx.Button(self, -1, '1'), 0, wx.EXPAND),
            (wx.Button(self, -1, '2'), 0, wx.EXPAND),
            (wx.Button(self, -1, '3'), 0, wx.EXPAND),
            (wx.Button(self, -1, '-'), 0, wx.EXPAND),
            (wx.Button(self, -1, '0'), 0, wx.EXPAND),
            (wx.Button(self, -1, '.'), 0, wx.EXPAND),
            (wx.Button(self, -1, '='), 0, wx.EXPAND),
            (wx.Button(self, -1, '+'), 0, wx.EXPAND) ])

      # Add sub-sizers to topSizer
      topSizer.Add(display, 0, wx.EXPAND|wx.ALL)
      topSizer.Add(ButtonSizer, 0, wx.ALL|wx.EXPAND, 5)

      self.panel.SetSizer(topSizer)

      # SetSizeHints(minW, minH, maxW, maxH)
      self.SetSizeHints(250,200,700,300)
      topSizer.Fit(self)
# Run the program
if __name__ == '__main__':
   app = wx.PySimpleApp()
   frame = MyForm().Show()
   app.MainLoop()

It should put the display at the top filling the sizer (worked before I added the other code) and the buttons down below it (resizeable). I am working under windows, so I am trying to put everything in a panel so it looks good.

Thank you all for your help.

-Brian

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org