如何编写wxPython textCtrl焦点事件 [英] How to write wxPython textCtrl focus event

查看:833
本文介绍了如何编写wxPython textCtrl焦点事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户单击textCtrl时触发一行代码.最终目标是在单击该框时突出显示该框的内容.我知道这可以通过wx.EVT_SET_FOCUS来实现,但是它可能是错误的,或者我实现错误.这是我的代码:

I'm trying to fire a line of code when the user clicks on a textCtrl. The end goal is to highlight the contents of the box when it is clicked on. I am aware that this is possible with wx.EVT_SET_FOCUS, but it's either buggy or I'm implementing it wrong. Here's my code:

self.m_textCtrl1 = wx.TextCtrl(self.m_panel2, wx.ID_ANY, wx.EmptyString, 
                               wx.DefaultPosition, wx.Size(100,-1), wx.TE_LEFT)
self.m_textCtrl1.SetMaxLength(8) 
self.m_textCtrl1.SetMinSize(wx.Size(100,-1))
self.m_textCtrl1.SetMaxSize(wx.Size(100,-1))
self.m_textCtrl1.Bind(wx.EVT_SET_FOCUS, self.highlightText, self.m_textCtrl1)

此代码能够在需要时成功触发HighlightText,但是由于某种原因,光标已从textCtrl中删除,从而使用户无法选择其位置,突出显示或退格键.任何建议,将不胜感激.附带说明一下,在wxFormBuilder中有没有办法做到这一点?我使用它构建了应用程序,但是无法添加焦点事件.似乎它提供的唯一焦点事件是针对整个窗口的.

This code is able to successfully fire highlightText when I want it to, but for some reason the cursor is removed from the textCtrl, leaving the user unable to pick his spot, highlight, or backspace. Any suggestions would be appreciated. As a side note, is there a way to do this in wxFormBuilder? I built my application using it but was unable to add a focus event. It seems the only focus events it offers are for the entire window.

编辑9/19/14: 迈克,这是我在gui.py中自动生成的wxFormBuilder代码:

EDIT 9/19/14: Mike, here's my automatically generated wxFormBuilder code, in gui.py:

class OrderNumEntry ( wx.Frame ):
    def __init__( self, parent ):
        # there's a lot more stuff here, but it's irrelevant
        self.m_textCtrl1.Bind( wx.EVT_SET_FOCUS, self.highlightText )

    def __del__( self ):
        pass

    # Virtual event handlers, overide them in your derived class
    def highlightText( self, event ):
        event.Skip()

...这是我编写的事件处理程序

... and here's the event handler that I wrote

import wx, gui

class OrderFrame(gui.OrderNumEntry):
    def __init__(self, parent):
        gui.OrderNumEntry.__init__(self, parent)
        # again, a lot more irrelevant stuff here

    def highlightText(self, event):
        print 'test'

该事件运行正常(如需要时可以在测试中打印出来),但是我无法突出显示文本,也看不到光标.

The event works fine (as in test is printed when I want it), but I'm not able to highlight text and I can't see my cursor.

推荐答案

您没有显示事件处理程序,但是我猜测您需要在其末尾调用event.Skip().我还想指出您错误地绑定了事件.应该是:

You don't show your event handler, but my guess would be that you need to call event.Skip() at the end of it. I want to also note that you binding the event incorrectly. It should be:

self.m_textCtrl1.Bind(wx.EVT_SET_FOCUS, self.highlightText)

self.Bind(wx.EVT_SET_FOCUS, self.highlightText, self.m_textCtrl1)

有关完整说明,请参见wxPython Wiki:

See the wxPython wiki for a complete explanation:

这篇关于如何编写wxPython textCtrl焦点事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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