I am new to Python. I am learning boxsizer. I want to put two buttons on
my panel. One at top right corner and one at bottom right corner. How do
I achieve this?


Recommended Answers

All 4 Replies

Try this:

''' wxBoxSizer101.py
button1 at upper right corner
button2 at lower right corner

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)

    def create_widgets(self):
        self.button1 = wx.Button(self, wx.ID_ANY, label='Button1')
        self.button2 = wx.Button(self, wx.ID_ANY, label='Button2')
        # use a box sizer to lay out widgets
        sizer_v = wx.BoxSizer(wx.VERTICAL)
        # Add(widget, proportion, flag, border)
        # border is to the left side
        sizer_v.Add(self.button1, 0, flag=wx.LEFT, border=200)
        # this adds a spacer (w, h)
        # here only the height is important
        sizer_v.Add((0, 200), proportion=0, flag=wx.EXPAND)
        sizer_v.Add(self.button2, 0, flag=wx.LEFT, border=200)


app = wx.App(0)
mytitle = "wx.BoxSizer Test101"
width = 300
height = 290
# create a MyFrame instance and show the frame
MyFrame(None, mytitle, (width, height)).Show()

Thanks for your reply. It works well for the window size mentioned. But if I maximize the window, the buttons remain at the same position.
Thanks again.

I figured it out. Thanks for your help.

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        vbox1 = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)

        mainbox = wx.BoxSizer(wx.VERTICAL)

        top_btn = wx.Button(self, wx.ID_ANY, "TOP")
        vbox1.Add(top_btn, 0, wx.ALL | wx.ALIGN_RIGHT, 1)

        bot_btn = wx.Button(self, wx.ID_ANY, "BOTTOM")
        hbox1.Add((0,0), 1, wx.ALL  | wx.EXPAND, 5)
        hbox1.Add(bot_btn, 0, wx.ALL | wx.ALIGN_BOTTOM, 5)

        mainbox.Add(vbox1, 0, wx.EXPAND)
        mainbox.Add((0,0), 1, wx.ALL|wx.EXPAND)
        mainbox.Add(hbox1, 0, wx.EXPAND)


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Button Box")
        panel1 = MyPanel(self)

if __name__ == "__main__":
    app = wx.App(False)
    frame_1 = MyFrame()

This fundamental wxPython application illustrates the five simple steps you have to complete for every wxPython program you develop:
Import the integral wxPython package.
Subclass the wxPython software class.
Define an application initialization method.
Create an application category instance.
Enter the application's essential event loop.

commented: Please be timely about replies to questions like this. 5 years ago they noted this was solved. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.