3
votes

Ok, so I'm learning about sizers in wxPython and I was wondering if it was possible to do something like:

==============================================
|WINDOW TITLE                          _ [] X|
|============================================|
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
|xxxxxxxxxxxxxxxxxxNOTEBOOKxxxxxxxxxxxxxxxxxx|
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
|________                         ___________|
|IMAGE   |                       |LoginForm  |
|________|                       |___________|
==============================================

NOTE:Yeah, I literally got this from wxPython - picking the right sizer to use in an application

With NOTEBOOK expanded to left and bottom, IMAGE to align to left and bottom and loginform align to right and bottom and I managed to do almost everything but now I have a problem..

The problem is that I can't align Loginform and Image separately (im using Box Sizers), and I would like to.

EDIT: So everyone can see what I mean: "Oh and what I was referring to, was basically that if I changed the align, it would affect both LoginForm and Image.. For example if I set the align to RIGHT, both image and loginform would have been aligned to the right because of: sizer.Add(sizer4,0, wx.ALIGN_RIGHT | wx.RIGHT, 10). Hope you guys can understand this time"

This is the code I'm using that is causing the problem at the moment, any help is appreciated. NOTE:The code might be (HUGELY) sloppy as I'm still learning box sizers. Heres a test code:

import wx
class Sizerframe(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'sizertestframe',size=(790, 524))
        p = wx.Panel(self)
        nb = wx.Notebook(p, size = (750, 332))
        button = wx.Button(p, -1, "loginform1rest", size=(94,23))
        button1 = wx.Button(p, -1, "Login", size=(94,23))
        button2 = wx.Button(p, -1, "Cancel", size=(94,23))
        imagebutton = wx.Button(p, -1, "imagebutton", size=(94,23))



        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer1.Add(nb,1, wx.EXPAND)
        sizer.Add(sizer1,1, wx.LEFT | wx.RIGHT | wx.EXPAND, 10)
        sizer.Add((-1, 25))
        sizer2 = wx.BoxSizer(wx.VERTICAL)
        sizer2.Add(button, 0)
        sizer3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer3.Add(button1, 0)
        sizer3.Add(button2,0, wx.LEFT, 5)
        sizer2.Add(sizer3, 0)

        sizer4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer4.Add(imagebutton, 1, wx.LEFT | wx.BOTTOM)
        sizer4.Add(sizer2,0, wx.RIGHT | wx.BOTTOM , 5)
        sizer.Add(sizer4,0, wx.ALIGN_RIGHT | wx.RIGHT, 10)
        p.SetSizer(sizer)

def main():
    app = wx.App()
    frame = Sizerframe()
    frame.Show()
    app.MainLoop()
if __name__ == '__main__':
    main()
2
wxGlade. Learn it, live it, love it. It's not perfect, but it's much better than hand-coding it from scratch. - Ignacio Vazquez-Abrams
Thanks for the advice, although I would still like to know if it was possible to solve my problem :). - dpswt
@Francisco – Could you post full code for a minimal but working example? Please, just as simple as possible, so no images, login boxes, etc, but just use buttons or panels. There are probably better examples, but I'm hoping for something like what I posted here: stackoverflow.com/questions/2546979/2549783#2549783 - tom10
@Francisco Aleixo, put a small code which we can run and experiment with, then it would be easy to put a solution - Anurag Uniyal
OK, but now your code and picture don't match because you've added in extra buttons. If it would get at your point, I suggest removing the extra buttons rather than adding them into the picture. Currently your example code has sizers nested fairly deeply, so it's difficult to guess what you're going for. - tom10

2 Answers

5
votes

How about something like this?

container = wx.BoxSizer(wx.VERTICAL)
container.Add(self.nb, 1, wx.EXPAND)

login = wx.BoxSizer(wx.VERTICAL)
login.Add(self.userLabel)
login.Add(self.userText)

# ... clip, rest of login form additions here

bottom = wx.BoxSizer(wx.HORIZONTAL)
bottom.Add(image)
bottom.Add((0, 0), 1, wx.EXPAND)
bottom.Add(login)

container.Add(bottom, 1, wx.EXPAND)

Basically, the bottom.Add((0, 0), 1, wx.EXPAND) will act as a spacer that'll take up all the space between the image and the login form. I didn't really understand what you meant by "The problem is that I can't align Loginform and Image separately (im using Box Sizers), and I would like to". I mostly just followed the illustration in trying to create a layout. I hope this helps.

0
votes

Just tagging on here, I've always found it's easier to make a skeleton of code in wxFormBuilder and then add the code to your project, as opposed to trial-and-error in pure Python.