wxpython 当 TextCtrl 焦点改变时调用函数

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

早上,我想在 TextCtrl 的内容发生更改时调用 GUI 中的函数。仅在用户离开 TextCtrl 对象之后,而不是在编辑期间。

请你帮我找到合适的事件处理程序来使用,我对 wxpython 很陌生,我什至找不到允许的事件列表。

非常感谢

python wxpython
2个回答
0
投票

您可以将

TextCtrl
绑定到
wx.EVT_TEXT
以捕获值的更改,并绑定
wx.EVT_KILL_FOCUS
以在焦点发生变化时触发事件。或者,您可以将其绑定到
wx.EVT_SET_FOCUS
并保存当前
TextCtrl
值,并将其绑定到
wx.EVT_KILL_FOCUS
并比较新值以查看它是否已更改。

--------编辑--------

WxFormBuilder 是快速创建基本 UI 的绝佳工具。它还会向您显示可以通过单击“事件”选项卡绑定到小部件的每个事件。


0
投票

我做了一个监听器,绑定键盘事件并将输入设置为数值,您可以将此监听器更改为您喜欢的行为(:

用途:

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.