[wxPython] My first attempt..why won't the buttons line up right?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] My first attempt..why won't the buttons line up right?

Stephen Pitts
I'm trying to write a simple flashcard program as an exercise in
learning wxPython. I'd like VCR-like controls on the bottom. and a
drawing area where the flashcard can draw text or an image on the top.
My first question: is there a specific control type (like Java or TK's
Canvas or VB's Image) that I need to create, or is just getting a
wxClientDC my best bet for drawing?

My problem is that the wxLayoutConstraints stuff is a little unclear.
The following snippet of code only produces one button, the '>>' one, at
the top left corner of the window. I want all of the buttons on the
bottom with equal widths based on the percentages I specified. What have
I done wrong? (check the FlashcardMain class, the XML parsing stuff
works OK)
--
Stephen Pitts
[hidden email]
webmaster - http://www.mschess.org

flashcard.py (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] My first attempt..why won't the buttons line up right?

Robin Dunn
> I'm trying to write a simple flashcard program as an exercise in
> learning wxPython. I'd like VCR-like controls on the bottom. and a
> drawing area where the flashcard can draw text or an image on the top.
> My first question: is there a specific control type (like Java or TK's
> Canvas or VB's Image) that I need to create, or is just getting a
> wxClientDC my best bet for drawing?
>

You can draw on any window type, but you probably want to put a class
derived from wxWindow or wxScrolledWindow in the frame for drawing upon
since the wxFrame will have platform settings that they have to conform to.

> My problem is that the wxLayoutConstraints stuff is a little unclear.
> The following snippet of code only produces one button, the '>>' one, at
> the top left corner of the window. I want all of the buttons on the
> bottom with equal widths based on the percentages I specified. What have
> I done wrong? (check the FlashcardMain class, the XML parsing stuff
> works OK)

The thing to remember about Layout Constraints is that you always have to
specify at least 4 of them for the window to be fully constrained.  The
position and the width and height has to be able to be calculated from the
constraints, if it can't then nothing is done for the window.  (It doesn't
move or change size, which is what you were seeing.)

The other problem you had was you switched the order of parameters to the
PercentOf method.

Here is a set of constraints that do what you want, notice I added the
height constraint for each one:

                self.first_card = wxButton(self, -1, "<<")
                c1 = wxLayoutConstraints()
                c1.width.PercentOf(self, wxWidth, 25)
                c1.height.AsIs()
                c1.left.SameAs(self, wxLeft)
                c1.bottom.SameAs(self, wxBottom)
                self.first_card.SetConstraints(c1)
                EVT_BUTTON(self.first_card, 1, self.OnFirstCard)

                self.prev_card = wxButton(self, -1, "<")
                c2 = wxLayoutConstraints()
                c2.left.RightOf(self.first_card)
                c2.width.PercentOf(self, wxWidth, 25)
                c2.height.SameAs(self.first_card, wxHeight)
                c2.bottom.SameAs(self, wxBottom)
                self.prev_card.SetConstraints(c2)
                EVT_BUTTON(self.prev_card, 2, self.OnPrevCard)

                self.next_card = wxButton(self, -1, ">")
                c3 = wxLayoutConstraints()
                c3.left.RightOf(self.prev_card)
                c3.width.PercentOf(self, wxWidth, 25)
                c3.height.SameAs(self.first_card, wxHeight)
                c3.bottom.SameAs(self, wxBottom)
                self.next_card.SetConstraints(c3)
                EVT_BUTTON(self.next_card, 3, self.OnNextCard)

                self.last_card = wxButton(self, -1, ">>")
                c4 = wxLayoutConstraints()
                c4.left.RightOf(self.next_card)
                c4.width.PercentOf(self, wxWidth, 25)
                c4.height.SameAs(self.first_card, wxHeight)
                c4.bottom.SameAs(self, wxBottom)
                self.last_card.SetConstraints(c4)
                EVT_BUTTON(self.last_card, 4, self.OnLastCard)


Another option is to use the new sizers.  As you can see it takes quite a
bit less code to do the same thing:

                self.canvas = wxWindow(self, -1)

                self.box = wxBoxSizer(wxVERTICAL)
                self.box.Add(self.canvas, 1)

                otherbox = wxBoxSizer(wxHORIZONTAL)
                otherbox.Add(self.first_card, 1)
                otherbox.Add(self.prev_card, 1)
                otherbox.Add(self.next_card, 1)
                otherbox.Add(self.last_card, 1)

                self.box.Add(otherbox, 0)


        def OnSize(self, event):
                size = self.GetClientSize()
                self.box.Layout(size)

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] My first attempt..why won't the buttons line up right?

Stephen Pitts
On Fri, Aug 27, 1999 at 11:52:29PM -0700, Robin Dunn wrote:
> The thing to remember about Layout Constraints is that you always have to
> specify at least 4 of them for the window to be fully constrained.  The
Ahhh..I must have skimmed through that part of the docs!

> Another option is to use the new sizers.  As you can see it takes quite a
> bit less code to do the same thing:
And just when I though that it couldn't get any better..here come the
sizers!

> --
> Robin Dunn
> Software Craftsman
I like that..has a better feel than 'Programmer'!

Thanks for all your help...I have a warm fuzzy feeling about wxPython
already!
--
Stephen Pitts
[hidden email]
webmaster - http://www.mschess.org

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users