Member Avatar for rbyrd
rbyrd

I am trying to learn wxpython, but having trouble laying out two buttons that I want to be centered in a horizontal panel. I can get the buttons on the panel, but they are not centered. What I'm trying to do is create a Frame that contains an upper panel (topPanel) and lower panel (botPanel). The buttons are on the lower panel. (The upper panel will eventually be used for drawing). For now, the buttons don't do anything but enable and disable. My understanding of wxpython has been gleaned from the internet, and there's much I don't understand, so I would appreciate any help and comments on the following code. Thanks.

import wx

class Hull(wx.Frame):
    def __init__(self,parent,title):
        noResize = wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.RESIZE_BOX | wx.MAXIMIZE_BOX)
        super(Hull,self).__init__(parent,style= noResize, title=title,size=(800,800))

        self.SetBackgroundColour((0,0,200))             # frame color

        self.topPanel = wx.Panel(self, size=(800,700))
        self.topPanel.SetBackgroundColour('Goldenrod')

        self.botPanel = wx.Panel(self)
        self.botPanel.SetBackgroundColour((150,150,200))

        self.btnGenerate = wx.Button(self.botPanel, id=wx.ID_ANY,label ='Generate Points')
        self.btnGenerate.Bind(wx.EVT_BUTTON, self.OnGenerate)

        self.btnDraw = wx.Button(self.botPanel,id=wx.ID_ANY,label='Draw Hull')
        self.btnDraw.Bind(wx.EVT_BUTTON, self.OnDraw)
        self.btnDraw.Enable(0)

        self.hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.hbox.Add(self.btnGenerate, 0, wx.ALL, 20)
        self.hbox.Add(self.btnDraw, 0, wx.ALL, 20)

        self.botPanel.SetSizer(self.hbox)

        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(self.topPanel,1, wx.EXPAND | wx.ALL)
        self.vbox.Add(self.botPanel,1, wx.EXPAND | wx.ALL, 5)  # 5 px border for botPanel
        self.SetSizer(self.vbox)

        self.Center()
        self.Show()

    def OnGenerate(self, e):
        self.btnGenerate.Enable(0)
        self.btnDraw.Enable(1)

    def OnDraw(self, e):
        self.btnGenerate.Enable(1)
        self.btnDraw.Enable(0)

if __name__ == '__main__':

    app = wx.App()
    Hull(None,title='HULL')
    app.MainLoop()
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.