wx.lib.masked.NumCtrl 的替代品

问题描述 投票:0回答:2

在我正在开发的 wxPython 应用程序中,我需要很多数字输入字段(整数和浮点数),所以我尝试使用 wx.lib.masked.NumCtrl,但我的用户现在告诉我,使用它非常不舒服(并且我同意他们的观点)。

是否有我可以使用的替代小部件实现,或者我应该从裸露的 TextCtrl 开始自己推出?

(wxPython 2.8.9.1)

编辑
为了完整起见,这里有一个“不舒服”的例子:
给定带有

selectOnEntry
fractionWidth > 0
的 NumCtrl,当您切换到字段的小数部分时,它会被正确选择,但按数字不会执行任何操作,您必须先删除字段的内容。

python wxpython
2个回答
1
投票

在通常的 wxPython 发行版中,有 IntCtrl,还有一些其他 GUI 控件,如 Slider、Spin、FloatSpin 和 KnobCtrl。

还有 Enthought Traits 方法,其中的 GUI 部分似乎将相当多的重点放在数字输入和显示上,例如 对数滑块浮点数组编辑器等。即使你不走这条路,他们的设计也可能会给你一些灵感。

另外,还不清楚为什么你不喜欢屏蔽的 NumCtrl,但是编写你自己的 NumCtrl 很容易,所以如果你想要一些特定的东西,这可能就是要走的路。


0
投票

您可以使用 TxtCtrl 并检查按下的用户键代码并仅添加数字键代码。 我亲自做到了这一点并管理了字段 ecc 的所有选择部分。

用途:

self.__MyTxtCtrl.Bind(wx.EVT_CHAR, self.__on_change_text_check_is_int_value)


def __on_change_text_check_is_int_value(self, evt):
        KeyboardEventUtils.on_change_text_check_is_int_value(self, evt)

单例类utils中的监听器:

from pyglet.window import key

FLOAT_POINT = "."

ENTRY_ID = "id"
ENTRY_KEY_EVENT = "keyEvent"
ENTRY_SELECTION_EVENT = "selectionEvent"

MASK_STARTED_RIGHT_SELECTION = 1
MASK_STARTED_LEFT_SELECTION = -1

class KeyboardEventUtils(object):

    __mLastEvt = {}

    def check_input_key_only_numeric_value(value, strng):
        return value.isnumeric() or (value == FLOAT_POINT and not FLOAT_POINT in strng and len(strng) > 0)

    def on_change_text_check_is_int_value(self, evt):
        txt = evt.GetEventObject()
        value = chr(evt.GetUnicodeKey())

        if KeyboardEventUtils.__mLastEvt != None and len(KeyboardEventUtils.__mLastEvt) > 0 and KeyboardEventUtils.__mLastEvt[ENTRY_ID] != txt.GetId():
            self._mLastEvt = {}

        if check_input_key_only_numeric_value(value, txt.GetValue()):
            pos = txt.GetInsertionPoint()
            txt.SetValue(txt.GetValue()[:pos] + value + txt.GetValue()[pos:])
            txt.SetInsertionPoint(pos + 1)

        elif (evt.GetModifiers() == 4 or evt.GetModifiers() == key.MOD_SHIFT) and evt.GetRawKeyCode() == key.LEFT:            # 4 (?) key.MOD_SHIFT (?)              TO FIX
            pos = txt.GetInsertionPoint()
            rng = list(txt.GetSelection())

            if rng[0] == rng[1] and ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt:
                del KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT]

            if ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt and KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] == MASK_STARTED_RIGHT_SELECTION:

                rng[1] = rng[1] - 1
            else:
                KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] = MASK_STARTED_LEFT_SELECTION
                rng[0] = rng[0] - 1

            txt.SetSelection(rng[0], rng[1])

        elif (evt.GetModifiers() == 4 or evt.GetModifiers() == key.MOD_SHIFT):            # 4 (?) key.MOD_SHIFT (?)             TO FIX
            pos = txt.GetInsertionPoint()
            rng = list(txt.GetSelection())

            if rng[0] == rng[1] and ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt:
                del KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT]

            if ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt and KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] == MASK_STARTED_LEFT_SELECTION:
                rng[0] = rng[0] + 1
            else:
                KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] = MASK_STARTED_RIGHT_SELECTION
                rng[1] = rng[1] + 1

            txt.SetSelection(rng[0], rng[1])

        elif evt.GetRawKeyCode() == key.LEFT:
            rng = list(txt.GetSelection())

            if rng[0] != rng[1]:
                txt.SetInsertionPoint(rng[0])
            else:
                txt.SetInsertionPoint(txt.GetInsertionPoint() - 1)

        elif evt.GetRawKeyCode() == key.RIGHT:
            rng = list(txt.GetSelection())

            if rng[0] != rng[1]:
                txt.SetInsertionPoint(rng[1])
            else:
                txt.SetInsertionPoint(txt.GetInsertionPoint() + 1)

        elif evt.GetRawKeyCode() == key.UP or evt.GetRawKeyCode() == key.DOWN:
            pass

        elif evt.GetRawKeyCode() == key.BACKSPACE:                                              
            pos = txt.GetInsertionPoint()
            if txt.GetStringSelection() == "":
                txt.SetValue(txt.GetValue()[:pos-1] + txt.GetValue()[pos:])
                txt.SetInsertionPoint(pos - 0x1)
            else:
                r = txt.GetSelection()
                txt.SetValue(txt.GetValue()[:r[0]] + txt.GetValue()[r[1]:])
                txt.SetInsertionPoint(pos)

        elif evt.GetRawKeyCode() == key.DELETE:
            pos = txt.GetInsertionPoint()
            if txt.GetStringSelection() == "":
                txt.SetValue(txt.GetValue()[:pos] + txt.GetValue()[pos+1:])
            else:
                r = txt.GetSelection()
                txt.SetValue(txt.GetValue()[:r[0]] + txt.GetValue()[r[1]:])
            txt.SetInsertionPoint(pos)

        elif evt.GetModifiers() == key.MOD_CTRL and evt.GetRawKeyCode() == key.A:
            txt.SelectAll()

        else:

            KeyboardEventUtils.__mLastEvt[ENTRY_ID] = txt.GetId()
            KeyboardEventUtils.__mLastEvt[ENTRY_KEY_EVENT] = evt
            return False

        KeyboardEventUtils.__mLastEvt[ENTRY_ID] = txt.GetId()
        KeyboardEventUtils.__mLastEvt[ENTRY_KEY_EVENT] = evt
        return True

希望能帮到你^^

© www.soinside.com 2019 - 2024. All rights reserved.