我正在尝试在wxPython中创建一个基本的日期选择器/日历。到目前为止,我已经成功了 将所有必要的小部件添加到我的网格中,但我很难得到它们 定位得当。虽然有很多我不太了解的事情, 我对wx.SpinCtrl小部件特别感到沮丧,这似乎是 虽然没有指明,但是被一个巨大的边界包围着。任何人都可以 告诉我如何删除它?我希望旋转控件只有几个像素 远离框架的顶部,文字'二月'的像素数相同 从控件的底部。我尝试过各种各样的方法但似乎没有 有任何影响。
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()
答案 0 :(得分:1)
看起来你没有完全确定sizer的添加签名。它应该是sizer.Add(小部件,比例,标志,边框)
您继续跳过比例,并且根本没有指定边框。如果你这样做了:
yearNavigationBox.Add(sc, 0, wx.EXPAND|wx.TOP, 5)
它会将微调器放在帧顶部下方5个像素处(或者在此帧之前添加任何小部件)。你还有一些其他的地方,你正在跳过比例标志,并在其位置放置其他随机的东西。你想要经历并改变它。以下是几个教程: