仅使用数值格式化 wx.TextCtrl - 浮点精度 [英] Formatting wx.TextCtrl with numeric values only - float precision

查看:35
本文介绍了仅使用数值格式化 wx.TextCtrl - 浮点精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的应用程序,它使用带有 wxFILTER_NUMERIC 的裸 TextCtrl 小部件,以便仅接受数值(请参阅下面的代码).

I have created a simple app which uses bare TextCtrl widgets with wxFILTER_NUMERIC in order to accept only numerical values (see code below).

无论用户如何输入,我都希望这些值具有固定的显示精度(例如,在指定 17.0 或 17.000000 值的情况下,将始终显示 17.0000).我发现在 wxPython 中有一个选项可以使用派生的 NumCtrl 小部件(http://wxpython.org/docs/api/wx.lib.masked.numctrl-module.html),它可以完成这项工作,但我找不到它在基本的 wxWidgets 库中.

I want the values to have a fixed display precision irrespective of how user inputs them (e.g. will always show 17.0000 in case of 17.0 or 17.000000 values being specified). I found that in wxPython there is an option to use a derived NumCtrl widget (http://wxpython.org/docs/api/wx.lib.masked.numctrl-module.html) which does the job but I can't find it in the basic wxWidgets libraries.

那么,是否有现成的解决方案可以在我根本找不到的地方使用,或者我是否必须编写一个函数,该函数将在插入后手动更改数字格式(处理 EVT_TEXT 和 EVT_TEXT_ENTER 事件并修改输入字符串)?

So, is there a ready solution for this available somewhere which I simply cannot find or do I have to write a function which will change the format of the number manually once it has been inserted (handling EVT_TEXT and EVT_TEXT_ENTER events and modifying the input string)?

到目前为止我所拥有的:

What I have so far:

事件表

BEGIN_EVENT_TABLE(MyFrame, wxFrame) 
    EVT_MENU(ID_Quit, MyFrame::OnQuit)
    EVT_MENU(ID_About, MyFrame::OnAbout)
    EVT_TEXT(ID_textBox, MyFrame::ValueChanged)
    EVT_TEXT_ENTER(ID_textBox, MyFrame::ValueChanged)
END_EVENT_TABLE()

MyFrame内文本框的实现

wxPanel *panel = new wxPanel(this, -1);

    _textBox1 = new wxTextCtrl(panel,
         ID_textBox, 
         "0.0000", 
         wxPoint(100, 100), 
         wxSize(80,20),
         wxTE_PROCESS_ENTER,
         wxTextValidator(wxFILTER_NUMERIC,&_myValue),
         wxTextCtrlNameStr); 

处理功能

void MyFrame::ValueChanged(wxCommandEvent& WXUNUSED(event))
{
    // modify the string or do something clever
}

采用的解决方案多亏了答案,问题已经解决.这是我的选择:

SOLUTION ADOPTED Thanks to the answer the problem has been resolved. Here's what I went with:

`

wxFloatingPointValidator<double> _val(2,&_myValue,wxNUM_VAL_ZERO_AS_BLANK);
    _val.SetRange(0.,100.);     // set allowable range

_textBox1 = new wxTextCtrl(panel,   // create a textCtrl
     ID_textBox, 
     "0.0000", 
     wxPoint(100, 30), 
     wxSize(80,20),
     wxTE_PROCESS_ENTER,
     _val,          // associate the text box with the desired validator
     wxTextCtrlNameStr); 

`

推荐答案

参见 wxFloatingPointValidator在 wxWidgets 2.9 中可用.

See wxFloatingPointValidator available in wxWidgets 2.9.

这篇关于仅使用数值格式化 wx.TextCtrl - 浮点精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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