在wxGrid中增加wxDataPickerCtrl控件

编写环境:

1、ActivePython-2.7.2.5-win32-x86

2、wxPython2.8-win32-unicode-2.8.12.1-py27

3、wxFormBuilder_v3.3.3-beta

 

以下代码是演示如何在wxGrid中增加wxDataPickerCtrl控件的代码,是我查了好久才找到的。

import wx 
import wx.grid 

class DatePickerCellEditor(wx.grid.PyGridCellEditor): 
    def __init__(self): 
        wx.grid.PyGridCellEditor.__init__(self) 

    def Create(self, parent, id, evtHandler): 
        """ 
        Called to create the control, which must derive from wx.Control. 
        *Must Override* 
        """ 
        self._tc = wx.DatePickerCtrl(parent, id, size=(120,-1), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY) 
        self.SetControl(self._tc) 

        if evtHandler: 
            self._tc.PushEventHandler(evtHandler) 


    def SetSize(self, rect): 
        """ 
        Called to position/size the edit control within the cell rectangle. 
        If you don't fill the cell (the rect) then be sure to override 
        PaintBackground and do something meaningful there. 
        """ 
        self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2, wx.SIZE_ALLOW_MINUS_ONE) 

    def BeginEdit(self, row, col, grid): 
        """ 
        Fetch the value from the table and prepare the edit control 
        to begin editing.  Set the focus to the edit control. 
        *Must Override* 
        """ 
        s = grid.GetTable().GetValue(row, col) 
        d = wx.DateTime.Now() 
        try: 
            d.SetDay(int(s[:2])) 
            d.SetMonth(int(s[2:4])-1) 
            d.SetYear(int(s[4:8])) 
            self._tc.SetValue(d) 
        except: 
            pass 
        self.startValue = s 
        print self.startValue 
        #print 'self.startValue', self.startValue, type(self.startValue) 
        self._tc.SetValue(d) 
        #self._tc.SetInsertionPointEnd() 
        self._tc.SetFocus() 
        #self._tc.SetSelection(0, self._tc.GetLastPosition()) 

    def EndEdit(self, row, col, grid): 
        """ 
        Complete the editing of the current cell. Returns True if the value 
        has changed.  If necessary, the control may be destroyed. 
        *Must Override* 
        """ 
        changed = False 

        #val = str(self._tc.GetValue()) 
        d = self._tc.GetValue() 
        print 'd', d 
        month = d.GetMonth() 
        month = month+1 
        month = str(month) 
        if len(month)<2: 
            month = '0'+month 
        #val = str(d.GetDay()) + '-'+month + '-'+str(d.GetYear())
        val = str(d.GetYear()) + '-' + month + '-' + str(d.GetDay()) 
        print 'val, self.startValue',val, self.startValue 
        if val != self.startValue: 
            changed = True 
            grid.GetTable().SetValue(row, col, val) # update the table 

        self.startValue = '' 
        #self._tc.SetValue('') 
        return changed 

    def Reset(self): 
        """ 
        Reset the value in the control back to its starting value. 
        *Must Override* 
        """ 
        self._tc.SetValue(self.startValue) 
        #self._tc.SetInsertionPointEnd() 

    def Clone(self): 
        """ 
        Create a new object which is the copy of this one 
        *Must Override* 
        """ 
        return DatePickerCellEditor() 

class TestFrame(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None, title="Grid Editor", 
                          size=(640,480)) 

        grid = wx.grid.Grid(self) 
        grid.CreateGrid(50,50) 
        grid.SetDefaultEditor(DatePickerCellEditor()) 


app = wx.PySimpleApp() 
frame = TestFrame() 
frame.Show() 
app.MainLoop() 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值