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()
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 1,685 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  update text variable on label with keypress knoxvilles_joker 3 901 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  How to read text in kivy textinput or Label jadel440 1 1,150 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 2,472 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  Get selected text inside an Entry Jerdup 0 732 Aug-20-2020, 12:42 PM
Last Post: Jerdup
  [Tkinter] Get the last entry in my text widget Pedroski55 3 1,804 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  [Tkinter] Get text entry in frame1, echo it in frame2 Pedroski55 8 1,237 Jul-10-2020, 11:51 PM
Last Post: Pedroski55
  [Kivy] Kivy text label won't shows up! AVD_01 1 1,298 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [Tkinter] Python 3 change label text gw1500se 6 1,868 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Tkinter] finding lines from a text entry Sutsro 2 883 May-04-2020, 09:19 AM
Last Post: Sutsro

Forum Jump:

User Panel Messages

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