WxPython:控制科学记数法中的数字 [英] WxPython: Control for numbers in scientific notation

查看:36
本文介绍了WxPython:控制科学记数法中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

确实 FloatSpin 确实支持科学记数法,但我认为在这种情况下自旋控制具有误导性.

解决方案

我会自己回答这个帖子以备将来参考.

  • lib.masked 不是要走的路,因为掩码总是要求输入具有有限的宽度,这是不能保证的,正如这里所指出的:

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

  • 对我来说,使用 wx.PyValidator 可以按需要完成.带有复选框的工作示例(在我的情况下,这是合适的,但验证也可以由任何事件触发):
<预><代码>进口 wx类 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)自我展示(真)def on_checkbox(self,event):如果 event.GetEventObject().GetValue() 和 self.Validate():打印这是一个浮动!"类 FloatValidator(wx.PyValidator):""" 这个验证器用于确保用户输入了一个浮点数进入 MyFrame 的文本控件."""def __init__(self):""" 标准构造函数."""wx.PyValidator.__init__(self)def克隆(自我):""" 标准克隆器.请注意,每个验证器都必须实现 Clone() 方法."""返回 FloatValidator()def 验证(自我,赢):textCtrl = self.GetWindow()num_string = textCtrl.GetValue()尝试:浮动(数字字符串)除了:打印不是浮点数!恢复到 1e0."textCtrl.SetValue("1e0")返回错误返回真def TransferToWindow(self):""" 将数据从验证器传输到窗口.默认实现返回False,表示出错发生了.我们只返回 True,因为我们不进行任何数据传输."""return True # 防止 wxDialog 抱怨.def TransferFromWindow(self):""" 将数据从窗口传输到验证器.默认实现返回False,表示出错发生了.我们只返回 True,因为我们不进行任何数据传输."""return True # 防止 wxDialog 抱怨.app = wx.App(False)frame = MyFrame(None, '浮动测试')app.MainLoop()

Validator 类的代码片段取自

wx.TextCtrl 和 wx.Validator

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.

解决方案

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:控制科学记数法中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆