wxpython高级部件calendarctrl用法示例

发布时间:2020-07-14编辑:脚本学堂
有关wxpython中高级复杂部件calendarctrl的用法,wxpython中使用calendarctrl创建日历代码,wxpython中文教程学习,不了解的朋友参考下。

在wxpython中的高级部件,位于 wx 目录的 lib 子目录下。
这些部件是用 python 写就的,它们满足到那些相关 C++ wx 库所未覆盖的需求。

专题教程:wxpython中文教程

calendarctrl部件

calendarctrl 是个用以处理日期有关事务的便利部件。

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#calendar.py

import wx
import wx.calendar as cal

class Calendar(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(400, 400))
       
        vbox = wx.BoxSizer(wx.VERTICAL)
       
        calend = cal.CalendarCtrl(self, -1, wx.DateTime_Now(),
                                  style = cal.CAL_SHOW_HOLIDAYS|
                                  cal.CAL_SEQUENTIAL_MONTH_SELECTION)
        vbox.Add(calend, 0, wx.EXPAND|wx.ALL, 20)
        self.Bind(cal.EVT_CALENDAR, self.OnCalSelected,
                  id=calend.GetId())
       
        vbox.Add((-1, 20))
       
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.text = wx.StaticText(self, -1, '日 期')
        hbox.Add(self.text)
        vbox.Add(hbox, 0, wx.LEFT, 8)
       
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        btn = wx.Button(self, -1, 'Ok')
        hbox2.Add(btn, 1)
        vbox.Add(hbox2, 0, wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, 20)

        self.Bind(wx.EVT_BUTTON, self.OnQuit, id=btn.GetId())
        self.Bind(wx.EVT_CLOSE, self.OnQuit)

       
        self.SetSizer(vbox)

        self.Show(True)
        self.Center()

    def OnCalSelected(self, event):
        date = event.GetDate()
        dt = str(date).split(' ')
        s = ' '.join(str(s) for s in dt)
        self.text.SetLabel(str(s))
       
    def OnQuit(self, event):
        self.Destroy()
       
app = wx.App()
Calendar(None, -1, 'calendar.py')
app.MainLoop()

可用的日历样式有:
 

CAL_SUNDAY_FIRST
CAL_NO_MONTH_CHANGE
CAL_HITTEST_HEADER
CAL_HITTEST_SURROUNDING_WEEK
CAL_MONDAY_FIRST
CAL_SEQUENTIAL_MONTH_SELECTION
CAL_HITTEST_DAY
CAL_BORDER_NONE
CAL_SHOW_HOLIDAYS
CAL_SHOW_SURROUNDING_WEEKS
CAL_HITTEST_INCMONTH
CAL_BORDER_SQUARE
CAL_NO_YEAR_CHANGE
CAL_HITTEST_NOWHERE
CAL_HITTEST_DECMONTH
CAL_BORDER_ROUND

wxpython高级部件calendarctrl
图:calendar.py