wxPython:wx.SpinCtrl边框管理
我正在尝试在 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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的 sizer 的 Add 签名不太正确。它应该是 sizer.Add(widget,ratio,flags,border)
你一直在跳过比例并且根本没有指定边框。如果您这样做:
它将把微调器放置在框架顶部下方 5 个像素处(或者在此之前添加的任何小部件)。您还有其他一些地方跳过比例标志并将其他随机内容放在其位置。你会想要经历并改变它。这里有一些教程:
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: