我正在构建一个窗口,我想在其中构建一个非常简单的日历。在_organizeData的循环中,每次有新月份时,我都想向 GridSizer 添加一些按钮,该按钮位于顶部带有文本的垂直框内。我想不出将这个垂直 BoxSizer (vBox) 添加到 scrollPanel。self.data 将是一个这样的结构,一个字典列表:
[{'date': '14-03-2021', 'xyValues': [[01:00, 02:00, ...], [18.5, 17.2, ...]]}, ...]
import wx
import wx.lib.scrolledpanel as scrolled
class GraphCalendar(wx.Frame):
def __init__(self, parent, data):
wx.Frame.__init__(self, parent)
self.data = data
self.consumption = []
self.text = wx.StaticText(self, wx.ID_ANY, label="This is a test")
self.scrollPanel = scrolled.ScrolledPanel(self, wx.ID_ANY, size=((400, 600)), style=wx.SUNKEN_BORDER)
self.scrollPanel.SetupScrolling()
self.topVerticalBox = wx.BoxSizer(wx.VERTICAL)
self.topVerticalBox.Add(self.text, flag=wx.EXPAND)
self.topVerticalBox.Add(self.scrollPanel)
self.Center()
self._organizeData()
def _organizeData(self):
currentMonth = ''
for i in range(0, len(self.data)):
words = self.data[i]['date'].split('-') # words -> [0]day, [1]month and [2]year.
if currentMonth != words[1]:
currentMonth = words[1]
vBox = wx.BoxSizer(wx.VERTICAL)
# Name of the month / year.
text = wx.StaticText(self, wx.ID_ANY, f"{(words[1])} of {words[2]}")
# Grid where the button of the days will be added
gridSizer = wx.GridSizer(rows=5, cols=5, hgap=5, vgap=5)
vBox.Add(text, wx.ID_ANY)
vBox.Add(gridSizer, wx.ID_ANY)
# Create the button of the day and add it to the gridSizer.
button = wx.Button(self, 1000 + i, label=words[0], size=((20, 20)))
button.Bind(wx.EVT_BUTTON, self.OnButtonClicked)
gridSizer.Add(button, border=5)
self.SetSizerAndFit(self.topVerticalBox)
def OnButtonClicked(self, event):
""" Funcao chamada quando um botao e apertado. """
event.GetEventObject()
print(event.GetId())
app = wx.App()
frame = GraphCalendar(None, data=[{'date': '14-03-2021', 'xyValues': [['01:00', '02:00'], [18.5, 17.2]]}, {'date': '15-04-2021', 'xyValues': [['01:00', '02:00'], [19.5, 18.2]]}])
frame.Show()
app.MainLoop()
这是所需的输出:
这是上面的代码产生的: