So far the move to wxPython has been going fairly smoothly. However, it's crucial that I be able to display items in the center of the screen (images, buttons, and radioboxes). I read about wx.ALIGN_CENTER_VERTICAL and wx.ALIGN_CENTER_HORIZONTAL, but the vertical option just doesn't seem to be working for me.

Below I've modified vegaseat's code for the radiobox. As it is, it doesn't align vertically but will align horizontally if I put that in instead.

Any help?

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent, wx.ID_ANY, size=(500, 360))
        # match to the number of radiobuttons
        self.mychoices = ['image1', 'image2', 'image3', 'image4']

        # create a box with 4 radio buttons, no labels here
        label_choices = [' ', ' ', ' ', ' ']
        self.radiobox = wx.RadioBox(self, wx.ID_ANY,
            " click on a button ",
            choices=label_choices, style=wx.VERTICAL)
        # bind mouse click to an action
        self.radiobox.Bind(wx.EVT_RADIOBOX, self.onAction)

        # show present selection
        self.onAction(None)
        
        self.verticalDisplay = wx.BoxSizer(wx.VERTICAL)
        self.verticalDisplay.Add(self.radiobox, 0, wx.ALIGN_CENTER_VERTICAL)
        
        self.SetSizer(self.verticalDisplay)

    def onAction(self, event):
        """show the selected choice"""
        index = self.radiobox.GetSelection()
        s = "Selected " + self.mychoices[index]
        # show the result in the frame title
        self.SetTitle(s)


app = wx.App(0)
MyFrame().Show()
app.MainLoop()

wx.ALIGN_CENTER_VERTICAL in the sizer associates with the radiobox widget and not the individual radiobuttons in the widget.

You have to change the style in radiobox to get a row or column layout for the radiobuttons.

If you go to individual radiobuttons rather than the radiobox, then you can size each radiobutton and accomplish what you want ...

# grouping individual radio buttons

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle):
        wx.Frame.__init__(self, parent, -1, mytitle, size=(300, 130))
        self.SetBackgroundColour('yellow')

        # rb1 is checked by default
        self.rb1 = wx.RadioButton(self, -1, 'Value A', style=wx.RB_GROUP)
        self.rb2 = wx.RadioButton(self, -1, 'Value B')
        self.rb3 = wx.RadioButton(self, -1, 'Value C')
        self.rb1.Bind(wx.EVT_RADIOBUTTON, self.show_val)
        self.rb2.Bind(wx.EVT_RADIOBUTTON, self.show_val)
        self.rb3.Bind(wx.EVT_RADIOBUTTON, self.show_val)
        
        # layout using a vertical boxsizer
        sizerv = wx.BoxSizer(wx.VERTICAL)
        # put a border around each radiobutton (all sides)
        # and align the radiobutton in the horizontal center
        sizerv.Add(self.rb1, 0, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 
            border=5)
        sizerv.Add(self.rb2, 0, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 
            border=5)
        sizerv.Add(self.rb3, 0, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 
            border=5)
        self.SetSizer(sizerv)
        
    def show_val(self, event):
        """get the value of each radiobuttonn and show in title bar"""
        state1 = self.rb1.GetValue()
        state2 = self.rb2.GetValue()
        state3 = self.rb3.GetValue()
        if state1 == True:
            self.SetTitle('rb1 checked')
        elif state2 == True:
            self.SetTitle('rb2 checked')            
        elif state3 == True:
            self.SetTitle('rb3 checked')


app = wx.App(0)
# create a MyFrame instance and show the frame
MyFrame(None, 'wx.RadioButton').Show()
app.MainLoop()

Thank you! That helps me align the radio buttons horizontally. But what I was really looking for was a way to align the whole set of buttons *vertically*. I.e. they should all be right in the middle of the screen, not right at the top. This doesn't seem to work for the individual buttons either.

I am a little confused. Are you talking about the frame being in the center of the screen?

Here is an example ...

# a wxPython general frame template

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("green")

        # create an input widget
        #
        # bind mouse or key event to an action
        #
        # create an output widget
        #

    def onAction(self, event):
        """ some action code"""
        pass


app = wx.App(0)
# create a MyFrame instance and show the frame
mytitle = 'my title'
width = 400
height = 300
frame = MyFrame(None, mytitle, (width, height))
# this will center the frame in the middle of the screen
frame.Center()
frame.Show()
app.MainLoop()

No. Sorry to be unclear. The program will run with the frame maximized (to full screen). The images (etc.) need to appear in the dead center of the frame, rather than up near the top of the frame. This is what I thought that wx.ALIGN_CENTER_VERTICAL would fix, but it does nothing when I try it.

Sorry, for experiments the frame always needs to be maximized -- I should have mentioned.

You might be thinking about something like this:

# a wxPython frame full display-screen size
# position widgets in the center

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        # the panel is needed for proper positioning
        panel = wx.Panel(self)
        panel.SetBackgroundColour("green")
        w, h = mysize
        
        self.mychoices = ['image1', 'image2', 'image3', 'image4']

        # create a box with 4 radio buttons, no labels here
        label_choices = [' ', ' ', ' ', ' ']
        self.radiobox = wx.RadioBox(panel, -1,
            " click on a button ",
            choices=label_choices,
            style=wx.VERTICAL)
        # bind mouse click to an action
        self.radiobox.Bind(wx.EVT_RADIOBOX, self.onAction)
        # show first selection
        self.onAction(None)
        
        rbw, rbh = self.radiobox.GetSize()
        # put the radiobox in the center of the display area
        # assume title bar takes about 25 pixels
        self.radiobox.SetPosition(((w-rbw)/2, (h-rbh)/2 - 25))

    def onAction(self, event):
        """show the selected choice"""
        index = self.radiobox.GetSelection()
        s = "Selected " + self.mychoices[index]
        # show the result in the frame title
        self.SetTitle(s)


app = wx.App(0)
# create a MyFrame instance and show the frame
mytitle = 'my title'
# make the frame full display-screen size and show
MyFrame(None, mytitle, wx.DisplaySize()).Show()
app.MainLoop()

In this case you need to calculate the center position for every widget you want to bring up. Easily done.

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.