| | |
wxPython Vertical Aligning failing
Thread Solved |
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?
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)
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()
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)
# 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()
May 'the Google' be with you!
I am a little confused. Are you talking about the frame being in the center of the screen?
Here is an example ...
Here is an example ...
python Syntax (Toggle Plain Text)
# 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()
May 'the Google' be with you!
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.
Sorry, for experiments the frame always needs to be maximized -- I should have mentioned.
You might be thinking about something like this:
In this case you need to calculate the center position for every widget you want to bring up. Easily done.
python Syntax (Toggle Plain Text)
# 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()
No one died when Clinton lied.
![]() |
Similar Threads
- Starting wxPython (GUI code) (Python)
- wxPython Scrolling Questions (Python)
- wxPython help please? (Python)
- Need help with wxPython GUI (Python)
- HTML table vertical aligning problem (HTML and CSS)
- Tkinter or wxPython? (Python)
- a simple notepad in wxPython (Python)
- notepad in wxPython (Python)
- wxpython gdk error on fedora 5 (fc5) (Python)
Other Threads in the Python Forum
- Previous Thread: Python with Cygwin
- Next Thread: How to handle an aborted C function called from Python
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






