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
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,669 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] bind menator01 1 1,234 Apr-15-2022, 08:47 PM
Last Post: menator01
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 7,624 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  update text variable on label with keypress knoxvilles_joker 3 4,841 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  How to read text in kivy textinput or Label jadel440 1 5,179 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 7,258 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  Get selected text inside an Entry Jerdup 0 2,443 Aug-20-2020, 12:42 PM
Last Post: Jerdup
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,295 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  [Tkinter] Get text entry in frame1, echo it in frame2 Pedroski55 8 3,861 Jul-10-2020, 11:51 PM
Last Post: Pedroski55
  [Kivy] Kivy text label won't shows up! AVD_01 1 2,894 Jun-21-2020, 04:01 PM
Last Post: AVD_01

Forum Jump:

User Panel Messages

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