`
dqifa
  • 浏览: 112092 次
社区版块
存档分类
最新评论

在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() 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics