wxPython Vertical Aligning failing

Thread Solved

Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

wxPython Vertical Aligning failing

 
0
  #1
Jun 1st, 2009
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?

  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()
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,015
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 930
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: wxPython Vertical Aligning failing

 
0
  #2
Jun 1st, 2009
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,015
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 930
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: wxPython Vertical Aligning failing

 
0
  #3
Jun 1st, 2009
If you go to individual radiobuttons rather than the radiobox, then you can size each radiobutton and accomplish what you want ...
  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()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: wxPython Vertical Aligning failing

 
0
  #4
Jun 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,015
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 930
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: wxPython Vertical Aligning failing

 
0
  #5
Jun 1st, 2009
I am a little confused. Are you talking about the frame being in the center of the screen?

Here is an example ...
  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()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: wxPython Vertical Aligning failing

 
0
  #6
Jun 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,276
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: wxPython Vertical Aligning failing

 
0
  #7
Jun 2nd, 2009
You might be thinking about something like this:
  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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC