Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [WxPython] bind label and entry text with return key
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):
        self.parent = parent

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

        self.entry = wx.TextCtrl(self,-1,value=u"Enter text here.")
        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 !')
        sizer.Add( self.label, (1,0),(1,2), wx.EXPAND )

        self.SetSizeHints(-1,self.GetSize().y,-1,self.GetSize().y );

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

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

if __name__ == "__main__":
    app = wx.App()
    frame = simpleapp_wx(None,-1,'my application')
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)
        self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.entry)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [WxPython] Bind error PeterLinux 1 93 7 hours ago
Last Post: joe_momma
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 121 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 920 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Label, align imported text from pandas kundrius 2 329 Dec-11-2019, 08:26 AM
Last Post: kundrius
  Make Label Text background (default color) transparent using tkinter in python barry76 1 3,459 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  [Tkinter] How to bind an event when enter is pressed on a Entry control? Michael4 4 461 Aug-29-2019, 10:11 PM
Last Post: Michael4
  Update a label text from subprocess jim53 3 832 Aug-19-2019, 08:21 PM
Last Post: Denni
  Unable to update or refresh label text in tkinter jenkins43 3 1,553 Jul-24-2019, 02:09 PM
Last Post: Friend
  [Tkinter] Returning Entry as a Label ashleyrfm94 2 420 Jul-22-2019, 04:47 PM
Last Post: Yoriz
  [Tkinter] getting Entry text Stauricus 4 721 May-10-2019, 08:24 PM
Last Post: swanysto

Forum Jump:

Users browsing this thread: 1 Guest(s)