WxPython:用科学记数法控制数字

时间:2014-01-24 14:56:33

标签: wxpython

是否有控件允许用户在WxPython中以科学记数法输入数字?我无法让NumCtrl接受这些,我也找不到格式化程序。

确实,FloatSpin确实支持科学记数法,但我认为旋转控制在这种情况下会产生误导。

2 个答案:

答案 0 :(得分:0)

您可以使用数据格式化程序完成您想要的任务。有一篇wxPython wiki文章实际上提到了一个使用科学记数法的文章:

或者,我建议查看matplotlib,它可以很容易地与wxPython集成。事实上,有人写了一篇关于在wx中创建公式编辑器的文章:

答案 1 :(得分: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()

带有Validator类的代码片段取自

wx.TextCtrl and wx.Validator