是否有控件允許用戶在WxPython中以科學記數法輸入數字?我無法讓NumCtrl接受這些,我也沒有找到格式化程序。WxPython:用科學計數法控制數字
事實上,FloatSpin確實支持科學記數法,但我認爲在這種情況下,旋轉控制是誤導性的。
是否有控件允許用戶在WxPython中以科學記數法輸入數字?我無法讓NumCtrl接受這些,我也沒有找到格式化程序。WxPython:用科學計數法控制數字
事實上,FloatSpin確實支持科學記數法,但我認爲在這種情況下,旋轉控制是誤導性的。
我會回答這個問題張貼自己以供將來參考。
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()
代碼段與所述驗證類是從
採取您可以使用數據格式化程序完成您想要的功能。有實際使用這裏科學記數法提到一個一個的wxPython wiki文章:
另外,我建議在看matplotlib,可與wxPython的輕鬆集成。事實上,有人寫了一篇關於這裏WX創建一個公式編輯器:
歡迎來到Stack Overflow!看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的細節越多,您可能收到的答案就越多。 – kkuilla