Reputation:
wxPython is very poorly documented and understanding sizers is abit difficult, ive had to print off code and study it piece by piece to understand what it does, and easier ways to memorize how it works.
Seeing as i come from a web background, i tend to compare sizers to xhtml div tags, and i compare css, to the flags and what not in wxpython, but seeing as divs and css is very well spaced out and whatnot, it is abit hard keeping up with everything in wxpython, but i guess time will tell.
Right here, i am trying to create a simple frame with a panel, and a static box with a label and a border of ten pixels, with a couple of static texts and a few text controls. (wx.TextCtrl)
This is what i aim to create.
I imagine the code wouldnt be too much, but with the amount of parents and flags and whatnot, it just gets way to confusing.
How can i go about adding a staticbox with padding so it isnt touching the sides of the panel (border) and how would i know what sizers i should be using?
Upvotes: 2
Views: 1017
Reputation: 76772
Here's a much more maintainable starting point for you.
import wx
class PersonalInfoDialog(wx.Dialog):
def __init__(self, parent):
super(PersonalInfoDialog, self).__init__(parent)
self.SetTitle('Personal Info')
sizer = self.create_controls()
self.SetSizer(sizer)
def create_controls(self):
box = self.create_box()
buttons = self.create_buttons()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(box, 1, wx.EXPAND | wx.ALL, 10)
sizer.Add(buttons, 0, wx.EXPAND | wx.BOTTOM, 10)
return sizer
def create_box(self):
contents = self.create_box_contents()
box = wx.StaticBox(self, -1, 'Personal Info')
sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
sizer.Add(contents, 1, wx.EXPAND | wx.ALL, 10)
return sizer
def create_box_contents(self):
male = wx.CheckBox(self, -1, 'Male')
married = wx.CheckBox(self, -1, 'Married')
age = self.create_age()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(male)
sizer.AddSpacer(10)
sizer.Add(married)
sizer.AddSpacer(10)
sizer.Add(age)
return sizer
def create_age(self):
age = wx.SpinCtrl(self, -1, '28', min=0, max=100, size=(64, -1))
sizer = wx.BoxSizer(wx.HORIZONTAL)
text = wx.StaticText(self, -1, 'Age')
sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL)
sizer.AddSpacer(10)
sizer.Add(age)
return sizer
def create_buttons(self):
button = wx.Button(self, wx.ID_OK, 'OK')
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.AddStretchSpacer(1)
sizer.Add(button)
sizer.AddStretchSpacer(1)
return sizer
def main():
app = wx.App(False)
dialog = PersonalInfoDialog(None)
dialog.ShowModal()
dialog.Destroy()
app.MainLoop()
if __name__ == '__main__':
main()
Upvotes: 3
Reputation: 113978
this will create basically what you want .. some minor modification is probably needed ..
import wx
a = wx.App(redirect=False)
f = wx.Frame(None,-1,"application")
p = wx.Panel(f,-1)
sb = wx.StaticBox(p,-1,"label")
sb_sz = wx.StaticBoxSizer(sb,wx.VERTICAL)
sb_sz.Add(wx.CheckBox(p,-1,"CB 1",size=(200,-1)),0,wx.ALL,10)
sb_sz.Add(wx.CheckBox(p,-1,"CB 2"),0,wx.ALL,10)
sb_sz.AddSpacer(7)
sz_h = wx.BoxSizer(wx.HORIZONTAL)
sz_h.Add(wx.StaticText(p,-1,"Some Label"),0,wx.ALL,10)
sz_h.Add(wx.Choice(p,-1,choices = ["A","B","C"]))
sb_sz.Add(sz_h)
sb_sz.AddSpacer(35)
sz = wx.BoxSizer(wx.VERTICAL)
sz.Add(sb_sz,0,wx.ALL,10)
p.SetSizer(sz)
sz_h2 = wx.BoxSizer(wx.HORIZONTAL)
sz_h2.Add(wx.Button(p,wx.ID_OK))
sz_h2.Add(wx.Button(p,wx.ID_CANCEL))
sz.Add(sz_h2,0,wx.ALIGN_RIGHT|wx.ALL,10)
sz2 = wx.BoxSizer()
f.SetSizer(sz2)
sz2.Add(p)
f.Layout()
f.Fit()
f.Show()
a.MainLoop()
Upvotes: 0