I’m trying to create a basic date-selector/calendar in wxPython. So far I’ve managed
to add all the necessary widgets to my grid, but I’m struggling to get them
positioned properly. Although there are numerous things I don’t fully understand,
I’m getting particularly frustrated with the wx.SpinCtrl widget, which seems to be
surrounded by a massive border despite the fact that none is specified. Can anyone
tell me how to remove this? I want the spin control to be just a couple of pixels
away from the top of the frame, with the text ‘February’ the same number of pixels away
from the bottom of the control. I’ve tried all manner of approaches but none seem to
have any effect.
import wx
import calendar
MONTH_NAME_AS_KEY = {"January":1, "February":2, "March":3, "April":4, "May":5, "June":6,
"July":7, "August":8, "September":9, "October":10, "November":11,
"December":12}
def getMonthNumber(nameAsString):
return MONTH_NAME_AS_KEY.get(nameAsString)
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,size=(300, 350))
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.panel = wx.Panel(self)
mainSizer = wx.BoxSizer(wx.VERTICAL)
fgs = wx.FlexGridSizer(rows=2, cols=2)
# fgs 1: blank space
filler = wx.StaticText(self.panel, -1, label="")
fgs.Add(filler)
# fgs 2: creating containers
vSizer = wx.BoxSizer(wx.VERTICAL)
yearNavigationBox = wx.BoxSizer(wx.HORIZONTAL)
monthNavigationBox = wx.BoxSizer(wx.HORIZONTAL)
# year navigation
sc = wx.SpinCtrl(self.panel,-1, "", size=(70,70))
sc.SetRange(1980,2060)
sc.SetValue(2011)
yearNavigationBox.Add(sc, wx.ALL, wx.EXPAND|wx.TOP)
# month navigation
monthDown = wx.StaticText(self.panel, -1, label="<")
monthUp = wx.StaticText(self.panel, -1, label=">")
monthName = wx.StaticText(self.panel, wx.ID_ANY, label="February")
monthNavigationBox.Add(monthDown)
monthNavigationBox.Add(monthName)
monthNavigationBox.Add(monthUp)
vSizer.Add(yearNavigationBox)
vSizer.Add(monthNavigationBox)
fgs.Add(vSizer)
# fgs 3: the date of the month writ large
date = wx.StaticText(self.panel, wx.ID_ANY, label='31', style=wx.ALIGN_CENTER)
dateFont = wx.Font(150, wx.DEFAULT, wx.NORMAL, wx.BOLD)
date.SetFont(dateFont)
fgs.Add(date)
# fgs 4: the date grid
dateSquareSizer = wx.GridSizer(rows=6, cols=7, hgap=1, vgap=6)
dayFont = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD)
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
for item in days:
day = wx.StaticText(self.panel, wx.ID_ANY, label=item)
day.SetFont(dayFont)
dateSquareSizer.Add(day)
y = 2012
m = getMonthNumber(monthName.GetLabel())
gridData = list(calendar.Calendar().itermonthdays(y, m))
while len(gridData)<43: gridData.append(0)
for i in gridData:
if i == 0: i=""
square = wx.StaticText(self.panel, wx.ID_ANY, label=str(i))
square.SetFont(dayFont)
dateSquareSizer.Add(square, wx.ID_ANY)
fgs.Add(dateSquareSizer)
mainSizer.Add(fgs, wx.ALL, 2)
self.panel.SetSizer(mainSizer)
mainSizer.Fit(self)
if __name__ == '__main__':
app = wx.App()
Example(None, title='')
app.MainLoop()
It looks like you don’t quite have the sizer’s Add signature correct. It should be sizer.Add(widget, proportion, flags, border)
You keep skipping the proportion and are not specifying a border at all. If you did this instead:
It will put the spinner 5 pixels below the top of the frame (or whatever widget is added before this one). You have a few other places where you’re skipping the proportion flag and putting other random stuff in its place. You’ll want to go through and change that. Here are a couple tutorials: