Ambush
Ambush

Reputation: 125

GenButton alignment wxpython

i am struggling to find an answer to the alignment of my GenButton, i have looked through the docs on wxpython website and cant see any option in there to address this, hopefully one of you guys can point me in the right direction, as i need to align my buttons to the center of the catpanel, here is my code so far.

import wx, os, os.path
from wx.lib.buttons import GenButton

class Home(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, size=(1024, 576),style=wx.NO_BORDER)
        self.SetBackgroundColour(wx.Colour(107, 109, 109))

        self.catpanel()


    def catpanel(self):
        homepanel = wx.Panel(self, wx.ALIGN_CENTER)
        homepanel.BackgroundColour = (86, 88, 88)
        homepanel.SetSize((1024, 40))
        GenButton(homepanel, id=wx.ID_ANY, label="Home", style=wx.NO_BORDER, size=(-1, 40))

        self.Centre()
        self.Show()

if __name__ == '__main__':

    app = wx.App()
    Home(None)
    app.MainLoop()

`

i am using windows, i also understand this can be acheived by using pos = wx.DefaultPosition but would like a more accurate wx way of doing this, could someone inspect the code and let me know if i am doing it right as i am new to wxpython / python in general

tnx

Upvotes: 2

Views: 190

Answers (1)

nepix32
nepix32

Reputation: 3177

GUI toolkits tend to be smart about size of controls: You specified the height of our GenButton instance but not the width (size=(-1, 40)). If you adapt the button width to the parent panel width, you will get what you want (size=(1024, 40)).

However, this is not what you should do, because you would use sizers. With the style wx.NO_BORDER for the wx.Frame you seem to have hit another snafu, where sizers do not work as expected together with GenButton.

class Home(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, style=wx.NO_BORDER)
        self.SetBackgroundColour(wx.Colour(107, 109, 109))

        homepanel = wx.Panel(self, -1)
        sz = wx.BoxSizer(wx.VERTICAL)
        btn = GenButton(homepanel, id=wx.ID_ANY, label="Home", style=wx.NO_BORDER, size=(-1, 40))
        sz.Add(btn, 0, wx.EXPAND|wx.ALL, 0)
        # Dummy placeholder with heigth already set
        sz.Add((-1, 400), 1)

        homepanel.SetSizer(sz)
        # try to uncomment next line: without resizing
        # the window will not be layouted
        self.SetSize((1024, -1))
        self.Centre()
        self.Show()

Upvotes: 2

Related Questions