WxPython: Control for numbers in scientific notati

2019-08-17 05:02发布

问题:

Are there controls to allow the user to enter numbers in scientific notation in WxPython? I could not get NumCtrl to accept those, neither have I found a formatter.

Indeed FloatSpin does support scientific notation, but I think a spin control is misleading in this case.

回答1:

You might be able to accomplish what you want using a data formatter. There is a wxPython wiki article that actually mentions one using scientific notation here:

  • http://wiki.wxpython.org/DataFormatters

Alternatively, I would recommend looking at matplotlib, which can be easily integrated with wxPython. In fact, someone wrote an article about creating an equation editor in wx here:

  • http://wiki.wxpython.org/MatplotlibEquationEditor


回答2:

I will answer this post myself for future reference.

  • lib.masked is not the way to go, since a mask always requires the input to have finite width, which is not guaranteed, as pointed out here:

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

  • For me, using wx.PyValidator worked out as desired. Working example with a checkbox (In my case this is suitable but validation can also be triggered by any event):

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

The code snippet with the Validator class is taken from

wx.TextCtrl and wx.Validator



标签: wxpython