Discussion:
Binding Event to TextCTRL (wxEVT_CHAR)
Lamonte
2008-07-04 15:53:32 UTC
Permalink
I'm having issues with my application and binding events. Robin Dunn
told me the following:

-------
Catch the EVT_CHAR event and if the keycode is wx.WXK_RETURN do nothing,
and call event.Skip() for everything else.
-------

I tried the following in the attachment also here:

import wx

class TwitterWidget(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id)
self.SetTitle("Twitter")
self.SetSize((219,240))
self.panel = TwitterWidgetPanel(self,wx.ID_ANY)
self.Show()

class TwitterWidgetPanel(wx.Panel):
def __init__(self,parent,id):
wx.Panel.__init__(self,parent,id)
self.addStaticBox()

def addStaticBox(self):
self.sb = wx.StaticBox(self, -1, "Twitter Status")
self.sbsizer = wx.StaticBoxSizer(self.sb, wx.VERTICAL)
table = self.addTextBoxes()
self.sbsizer.Add(table,1,wx.EXPAND,25)
a = wx.BoxSizer()
a.Add(self.sbsizer, 1, wx.EXPAND, 25)
self.SetSizer(a)

def addTextBoxes(self):
#twitter status box
self.tb1_text = wx.StaticText(self, -1, "Add a new twitter status:")
self.tb1 = wx.TextCtrl(self, -1, "",size=(200, 100),
style=wx.TE_MULTILINE)
self.tb1.Bind(wx.EVT_CHAR,self.onKeyDown)
#twitter state box
state = ['Reading','Looking at','Listening
To','Watching','Pissed at','Chilling at','Coding']
x = 0
while x < len(state):
state[x] = state[x] + ':'
x = x + 1
self.tb2_text = wx.StaticText(self, -1, "Twitter State:")
self.tb2 = wx.Choice(self, -1, (100, 50), choices = state)
#twitter submit button
self.tb3 = wx.Button(self, -1, "Add New Status")
self.Bind(wx.EVT_BUTTON,self.onSubmitTwit,self.tb3) #gets our
values for each required input
#
table = wx.FlexGridSizer(cols=1, hgap=4, vgap=4)
table.AddMany([ self.tb2_text, self.tb2, self.tb1_text,
self.tb1,self.tb3 ])
return table
def onSubmitTwit(self, event):
print self.tb1.GetValue()
def onKeyDown(self,event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_RETURN:
event.Skip()

app = wx.App()
twit = TwitterWidget(None,wx.ID_ANY)
app.MainLoop()

It catches my event, but it doesn't do anything. Please help.
Cody Precord
2008-07-04 16:07:48 UTC
Permalink
Hello,
Post by Lamonte
I'm having issues with my application and binding events. Robin
-------
Catch the EVT_CHAR event and if the keycode is wx.WXK_RETURN do
nothing, and call event.Skip() for everything else.
-------
keycode = event.GetKeyCode()
event.Skip()
app = wx.App()
twit = TwitterWidget(None,wx.ID_ANY)
app.MainLoop()
It should be (keycode != wx.WXK_RETURN) if you want to block the
Return key and let everything else through.
Post by Lamonte
It catches my event, but it doesn't do anything. Please help.
(p.s) It is helpful to those those reading your message if you can
clearly state the problem and have near the top of the message instead
of buried between multiple blocks of code.


Cody
FT
2008-07-04 16:11:50 UTC
Permalink
----- Original Message -----
From: "Lamonte" <***@gmail.com>
To: <wxpython-***@lists.wxwidgets.org>
Sent: Friday, July 04, 2008 11:53 AM
Subject: [wxpython-users] Binding Event to TextCTRL (wxEVT_CHAR)

Hi!

I am not sure what your doing the email took out the tabs, so I added
the spaces and also included the else: it should work now.


I'm having issues with my application and binding events. Robin Dunn
told me the following:

-------
Catch the EVT_CHAR event and if the keycode is wx.WXK_RETURN do nothing,
and call event.Skip() for everything else.
-------

I tried the following in the attachment also here:

import wx

class TwitterWidget(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id)
self.SetTitle("Twitter")
self.SetSize((219,240))
self.panel = TwitterWidgetPanel(self,wx.ID_ANY)
self.Show()
___Snip___

def onKeyDown(self,event):
keycode = event.GetKeyCode()

if keycode == wx.WXK_RETURN:
#Doing something?

else:
event.Skip()

app = wx.App()
twit = TwitterWidget(None,wx.ID_ANY)
app.MainLoop()

Loading...