2009-10-14 39 views
1

我有一個帶日期選取器控件的對話框。點擊進入日期選擇器關閉對話框(如預期)。但是,日期選取器不會接收用戶在wxGTK上輸入的值。運行附加的示例,單擊按鈕,在日期選擇器中輸入新日期,使用鍵盤,然後按回車。 print語句顯示日期選擇器的前一個值,而不是剛輸入的值。對話框中的wx.DatePickerCtrl忽略在wxGTK上返回後輸入的值

在wxMSW上它按預期工作。在這兩種情況下使用wxPython 2.8.10.1。

任何想法?變通?

謝謝,弗蘭克

import wx 

class Dialog(wx.Dialog): 
    def __init__(self, *args, **kwargs): 
     super(Dialog, self).__init__(*args, **kwargs) 
     self.datepicker = wx.DatePickerCtrl(self) 
     self.button = wx.Button(self, label='OK') 
     self.button.SetDefault() 
     self.button.Bind(wx.EVT_BUTTON, self.onButton) 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.datepicker) 
     sizer.Add(self.button) 
     self.SetSizerAndFit(sizer) 

    def onButton(self, event): 
     print self.datepicker.GetValue() 
     self.Close() 


class Frame(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     super(Frame, self).__init__(*args, **kwargs) 
     self.button = wx.Button(self, label='Press me to open dialog') 
     self.button.Bind(wx.EVT_BUTTON, self.onButton) 

    def onButton(self, event): 
     dialog = Dialog(self, size=(200,30)) 
     dialog.Show() 

app = wx.App() 
frame = Frame(None, size=(200,50)) 
frame.Show() 
app.MainLoop() 

回答

1

我也有類似的問題。我試圖每次只需撥打datepicker.Navigate(),然後從DatePickerCtrl中檢索日期爲datepicker.GetValue()。它似乎工作。

+0

這聽起來確實比我的工作更簡單,謝謝! – 2010-01-21 21:52:39

0

這似乎是工作,解決此問題:

import wx 

class Dialog(wx.Dialog): 
    def __init__(self, *args, **kwargs): 
     super(Dialog, self).__init__(*args, **kwargs) 
     self.datepicker = wx.DatePickerCtrl(self) 

     # On wxGTK, intercept all keys: 
     if wx.Platform == '__WXGTK__': 
      comboCtrl = self.datepicker.GetChildren()[0] 
      comboCtrl.Bind(wx.EVT_KEY_DOWN, self.onKey) 

     self.button = wx.Button(self, label='OK') 
     self.button.SetDefault() 
     self.button.Bind(wx.EVT_BUTTON, self.onButton) 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.datepicker) 
     sizer.Add(self.button) 
     self.SetSizerAndFit(sizer) 

    def onButton(self, event): 
     print self.datepicker.GetValue() 
     self.Close() 

    def onKey(self, event): 
     keyCode = event.GetKeyCode() 
     if keyCode == wx.WXK_RETURN: 
      # Move to the next field so that the contents of the text control, 
      # that might be edited by the user, are updated by the datepicker: 
      self.datepicker.Navigate() 
      # Next, click the default button of the dialog: 
      button = self.GetDefaultItem() 
      click = wx.CommandEvent() 
      click.SetEventType(wx.EVT_BUTTON.typeId) 
      wx.PostEvent(button, click) 
     elif keyCode == wx.WXK_TAB: 
      self.datepicker.Navigate(not event.ShiftDown()) 
     else: 
      event.Skip() 


class Frame(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     super(Frame, self).__init__(*args, **kwargs) 
     self.button = wx.Button(self, label='Press me to open dialog') 
     self.button.Bind(wx.EVT_BUTTON, self.onButton) 

    def onButton(self, event): 
     dialog = Dialog(self, size=(200,30)) 
     dialog.Show() 

app = wx.App() 
frame = Frame(None, size=(200,50)) 
frame.Show() 
app.MainLoop()