Python Forum
[WxPython] bind label and entry text with return key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[WxPython] bind label and entry text with return key
#1
Im going through a tutorial comparing tkinter with wxpython, and this is the code. My problem is line 14 where it binds the label and entry text when return is pressed. It should change the label but it does not. wx.EVT_TEXT_ENTER appears to be the correct flag for this, but i am not sure why it does not work as described. My initial thought was this is out of date, but other examples from googling follow the same method.

import wx

class simpleapp_wx(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent,id,title)
        self.parent = parent
        self.initialize()

    def initialize(self):
        sizer = wx.GridBagSizer()

        self.entry = wx.TextCtrl(self,-1,value=u"Enter text here.")
        sizer.Add(self.entry,(0,0),(1,1),wx.EXPAND)
        self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.entry)

        button = wx.Button(self,-1,label="Click me !")
        sizer.Add(button, (0,1))
        self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button)


        self.label = wx.StaticText(self,-1,label=u'Hello !')
        self.label.SetBackgroundColour(wx.BLUE)
        self.label.SetForegroundColour(wx.WHITE)
        sizer.Add( self.label, (1,0),(1,2), wx.EXPAND )

        sizer.AddGrowableCol(0)
        self.SetSizerAndFit(sizer)
        self.SetSizeHints(-1,self.GetSize().y,-1,self.GetSize().y );
        self.entry.SetFocus()
        self.entry.SetSelection(-1,-1)
        self.Show(True)

    def OnButtonClick(self,event):
        self.label.SetLabel( self.entry.GetValue() + " (You clicked the button)" )
        self.entry.SetFocus()
        self.entry.SetSelection(-1,-1)

    def OnPressEnter(self,event):
        self.label.SetLabel( self.entry.GetValue() + " (You pressed ENTER)" )
        self.entry.SetFocus()
        self.entry.SetSelection(-1,-1)

if __name__ == "__main__":
    app = wx.App()
    frame = simpleapp_wx(None,-1,'my application')
    app.MainLoop()
Recommended Tutorials:
Reply
#2
i think i figured it out.

apparently you have to have wx.TE_PROCESS_ENTER as a style for the textctrl
        self.entry = wx.TextCtrl(self,-1,value=u"Enter text here.", style=wx.TE_PROCESS_ENTER)
        sizer.Add(self.entry,(0,0),(1,1),wx.EXPAND)
        self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.entry)
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to update label text using a grid button. Edward_ 7 1,508 Dec-18-2024, 03:05 AM
Last Post: Edward_
  update text variable on label with keypress knoxvilles_joker 5 7,523 May-31-2024, 02:09 PM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 10,050 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] bind menator01 1 1,867 Apr-15-2022, 08:47 PM
Last Post: menator01
  How to move in entries using the arrow keys by applying the bind and focus? pymn 4 9,121 Apr-06-2022, 04:29 AM
Last Post: pymn
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 10,960 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  How to read text in kivy textinput or Label jadel440 1 6,438 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 11,604 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  Get selected text inside an Entry Jerdup 0 3,539 Aug-20-2020, 12:42 PM
Last Post: Jerdup
  [Tkinter] Get the last entry in my text widget Pedroski55 3 8,579 Jul-13-2020, 10:34 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020