943,836 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1705
  • Python RSS
Jun 1st, 2009
0

wxPython Vertical Aligning failing

Expand Post »
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?

Python Syntax (Toggle Plain Text)
  1. import wx
  2.  
  3. class MyFrame(wx.Frame):
  4. def __init__(self, parent=None):
  5. wx.Frame.__init__(self, parent, wx.ID_ANY, size=(500, 360))
  6. # match to the number of radiobuttons
  7. self.mychoices = ['image1', 'image2', 'image3', 'image4']
  8.  
  9. # create a box with 4 radio buttons, no labels here
  10. label_choices = [' ', ' ', ' ', ' ']
  11. self.radiobox = wx.RadioBox(self, wx.ID_ANY,
  12. " click on a button ",
  13. choices=label_choices, style=wx.VERTICAL)
  14. # bind mouse click to an action
  15. self.radiobox.Bind(wx.EVT_RADIOBOX, self.onAction)
  16.  
  17. # show present selection
  18. self.onAction(None)
  19.  
  20. self.verticalDisplay = wx.BoxSizer(wx.VERTICAL)
  21. self.verticalDisplay.Add(self.radiobox, 0, wx.ALIGN_CENTER_VERTICAL)
  22.  
  23. self.SetSizer(self.verticalDisplay)
  24.  
  25. def onAction(self, event):
  26. """show the selected choice"""
  27. index = self.radiobox.GetSelection()
  28. s = "Selected " + self.mychoices[index]
  29. # show the result in the frame title
  30. self.SetTitle(s)
  31.  
  32.  
  33. app = wx.App(0)
  34. MyFrame().Show()
  35. app.MainLoop()
Similar Threads
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Jun 1st, 2009
0

Re: wxPython Vertical Aligning failing

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 1st, 2009
0

Re: wxPython Vertical Aligning failing

If you go to individual radiobuttons rather than the radiobox, then you can size each radiobutton and accomplish what you want ...
python Syntax (Toggle Plain Text)
  1. # grouping individual radio buttons
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent, mytitle):
  7. wx.Frame.__init__(self, parent, -1, mytitle, size=(300, 130))
  8. self.SetBackgroundColour('yellow')
  9.  
  10. # rb1 is checked by default
  11. self.rb1 = wx.RadioButton(self, -1, 'Value A', style=wx.RB_GROUP)
  12. self.rb2 = wx.RadioButton(self, -1, 'Value B')
  13. self.rb3 = wx.RadioButton(self, -1, 'Value C')
  14. self.rb1.Bind(wx.EVT_RADIOBUTTON, self.show_val)
  15. self.rb2.Bind(wx.EVT_RADIOBUTTON, self.show_val)
  16. self.rb3.Bind(wx.EVT_RADIOBUTTON, self.show_val)
  17.  
  18. # layout using a vertical boxsizer
  19. sizerv = wx.BoxSizer(wx.VERTICAL)
  20. # put a border around each radiobutton (all sides)
  21. # and align the radiobutton in the horizontal center
  22. sizerv.Add(self.rb1, 0, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL,
  23. border=5)
  24. sizerv.Add(self.rb2, 0, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL,
  25. border=5)
  26. sizerv.Add(self.rb3, 0, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL,
  27. border=5)
  28. self.SetSizer(sizerv)
  29.  
  30. def show_val(self, event):
  31. """get the value of each radiobuttonn and show in title bar"""
  32. state1 = self.rb1.GetValue()
  33. state2 = self.rb2.GetValue()
  34. state3 = self.rb3.GetValue()
  35. if state1 == True:
  36. self.SetTitle('rb1 checked')
  37. elif state2 == True:
  38. self.SetTitle('rb2 checked')
  39. elif state3 == True:
  40. self.SetTitle('rb3 checked')
  41.  
  42.  
  43. app = wx.App(0)
  44. # create a MyFrame instance and show the frame
  45. MyFrame(None, 'wx.RadioButton').Show()
  46. app.MainLoop()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 1st, 2009
0

Re: wxPython Vertical Aligning failing

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.
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Jun 1st, 2009
0

Re: wxPython Vertical Aligning failing

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

Here is an example ...
python Syntax (Toggle Plain Text)
  1. # a wxPython general frame template
  2.  
  3. import wx
  4.  
  5. class MyFrame(wx.Frame):
  6. def __init__(self, parent, mytitle, mysize):
  7. wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
  8. self.SetBackgroundColour("green")
  9.  
  10. # create an input widget
  11. #
  12. # bind mouse or key event to an action
  13. #
  14. # create an output widget
  15. #
  16.  
  17. def onAction(self, event):
  18. """ some action code"""
  19. pass
  20.  
  21.  
  22. app = wx.App(0)
  23. # create a MyFrame instance and show the frame
  24. mytitle = 'my title'
  25. width = 400
  26. height = 300
  27. frame = MyFrame(None, mytitle, (width, height))
  28. # this will center the frame in the middle of the screen
  29. frame.Center()
  30. frame.Show()
  31. app.MainLoop()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 2nd, 2009
0

Re: wxPython Vertical Aligning failing

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.
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Jun 2nd, 2009
0

Re: wxPython Vertical Aligning failing

You might be thinking about something like this:
python Syntax (Toggle Plain Text)
  1. # a wxPython frame full display-screen size
  2. # position widgets in the center
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. def __init__(self, parent, mytitle, mysize):
  8. wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
  9. # the panel is needed for proper positioning
  10. panel = wx.Panel(self)
  11. panel.SetBackgroundColour("green")
  12. w, h = mysize
  13.  
  14. self.mychoices = ['image1', 'image2', 'image3', 'image4']
  15.  
  16. # create a box with 4 radio buttons, no labels here
  17. label_choices = [' ', ' ', ' ', ' ']
  18. self.radiobox = wx.RadioBox(panel, -1,
  19. " click on a button ",
  20. choices=label_choices,
  21. style=wx.VERTICAL)
  22. # bind mouse click to an action
  23. self.radiobox.Bind(wx.EVT_RADIOBOX, self.onAction)
  24. # show first selection
  25. self.onAction(None)
  26.  
  27. rbw, rbh = self.radiobox.GetSize()
  28. # put the radiobox in the center of the display area
  29. # assume title bar takes about 25 pixels
  30. self.radiobox.SetPosition(((w-rbw)/2, (h-rbh)/2 - 25))
  31.  
  32. def onAction(self, event):
  33. """show the selected choice"""
  34. index = self.radiobox.GetSelection()
  35. s = "Selected " + self.mychoices[index]
  36. # show the result in the frame title
  37. self.SetTitle(s)
  38.  
  39.  
  40. app = wx.App(0)
  41. # create a MyFrame instance and show the frame
  42. mytitle = 'my title'
  43. # make the frame full display-screen size and show
  44. MyFrame(None, mytitle, wx.DisplaySize()).Show()
  45. app.MainLoop()
In this case you need to calculate the center position for every widget you want to bring up. Easily done.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python with Cygwin
Next Thread in Python Forum Timeline: How to handle an aborted C function called from Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC