2014-01-24 21 views
0

是否有控件允許用戶在WxPython中以科學記數法輸入數字?我無法讓NumCtrl接受這些,我也沒有找到格式化程序。WxPython:用科學計數法控制數字

事實上,FloatSpin確實支持科學記數法,但我認爲在這種情況下,旋轉控制是誤導性的。

+1

歡迎來到Stack Overflow!看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的細節越多,您可能收到的答案就越多。 – kkuilla

回答

0

我會回答這個問題張貼自己以供將來參考。

  • lib.masked是不是要走的路,因爲面具總是需要輸入具有有限的寬度,這是不保證的,因爲在這裏指出:

http://wxpython-users.1045709.n5.nabble.com/howto-use-validRegex-validFunc-mask-textctrl-td2370136.html

  • 對我來說,使用wx.PyValidator可以根據需要制定出來。工作實施例 一個複選框(在我的情況下,這是合適的,但驗證也可以由任何事件觸發):

    import wx 
    class MyFrame(wx.Frame): 
     def __init__(self, parent, title): 
      wx.Frame.__init__(self, parent, title=title) 
      self.box = wx.BoxSizer(wx.HORIZONTAL) 
      self.checkbox = wx.CheckBox(self, wx.ID_ANY, 'Float?') 
      self.txtcontrol = wx.TextCtrl(self, wx.ID_ANY, validator=FloatValidator()) 
      self.Bind(wx.EVT_CHECKBOX,self.on_checkbox,self.checkbox) 
      self.box.Add(self.checkbox,1) 
      self.box.Add(self.txtcontrol,1) 
      self.SetSizerAndFit(self.box) 

      self.Show(True) 

     def on_checkbox(self,event): 
      if event.GetEventObject().GetValue() and self.Validate(): 
       print "This is a float!" 


    class FloatValidator(wx.PyValidator): 
     """ This validator is used to ensure that the user has entered a float 
      into the text control of MyFrame. 
     """ 
     def __init__(self): 
      """ Standard constructor. 
      """ 
      wx.PyValidator.__init__(self) 



     def Clone(self): 
      """ Standard cloner. 

       Note that every validator must implement the Clone() method. 
      """ 
      return FloatValidator() 

     def Validate(self, win): 
      textCtrl = self.GetWindow() 
      num_string = textCtrl.GetValue() 
      try: 
       float(num_string) 
      except: 
       print "Not a float! Reverting to 1e0." 
       textCtrl.SetValue("1e0") 
       return False 
      return True 


     def TransferToWindow(self): 
      """ Transfer data from validator to window. 

       The default implementation returns False, indicating that an error 
       occurred. We simply return True, as we don't do any data transfer. 
      """ 
      return True # Prevent wxDialog from complaining. 


     def TransferFromWindow(self): 
      """ Transfer data from window to validator. 

       The default implementation returns False, indicating that an error 
       occurred. We simply return True, as we don't do any data transfer. 
      """ 
      return True # Prevent wxDialog from complaining. 

    app = wx.App(False) 
    frame = MyFrame(None, 'Float Test') 
    app.MainLoop() 

代碼段與所述驗證類是從

wx.TextCtrl and wx.Validator

採取
0

您可以使用數據格式化程序完成您想要的功能。有實際使用這裏科學記數法提到一個一個的wxPython wiki文章:

另外,我建議在看matplotlib,可與wxPython的輕鬆集成。事實上,有人寫了一篇關於這裏WX創建一個公式編輯器:

+0

感謝您的建議。編寫數據格式化程序可能是正確的選擇。 – alodi

+0

我使用了一個驗證器。格式化程序類也可以這樣做,但它旨在將接口數據轉換爲存儲數據,反之亦然。 – alodi