wxpython textctrl_如何编写wxPython textCtrl焦点事件

1586010002-jmsa.png

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)

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.

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.

解决方案

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)

or

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

See the wxPython wiki for a complete explanation:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值