Python Forum
[WxPython] [SOLVED] How to change button label? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [WxPython] [SOLVED] How to change button label? (/thread-37360.html)



[SOLVED] How to change button label? - Winfried - May-31-2022

Hello,

I can't figure out how to change a button's label when clicking on it:

import wx

class MyFrame(wx.Frame):    
    def __init__(self):
        super().__init__(parent=None, title='Hello World')
        panel = wx.Panel(self)        
        my_sizer = wx.BoxSizer(wx.VERTICAL)        

        my_btn = wx.Button(panel, label='Press Me')
        my_btn.Bind(wx.EVT_BUTTON, self.on_press)
        my_sizer.Add(my_btn, 0, wx.ALL | wx.CENTER, 5)        
        panel.SetSizer(my_sizer)        
        self.Show()

    def on_press(self, event):
		#self.SetLabel("New Value")
		#my_btn.SetLabel("blah")
		#self.SetLabel(my_btn,"New Value")
		self.SetLabel(self.my_btn,"New Value")

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()
Do I need some kind of refresh/redraw?

Thank you.


RE: [wxPython] How to change button label? - Yoriz - May-31-2022

To be able to have a pointer to the button object in the event handler method on_press you need to add self. to the front of my_btn in the __init__ method.
Then you can use the buttons SetLabel method.
import wx


class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title="Hello World")
        panel = wx.Panel(self)
        my_sizer = wx.BoxSizer(wx.VERTICAL)

        self.my_btn = wx.Button(panel, label="Press Me")
        self.my_btn.Bind(wx.EVT_BUTTON, self.on_press)
        my_sizer.Add(self.my_btn, 0, wx.ALL | wx.CENTER, 5)
        panel.SetSizer(my_sizer)
        self.Show()

    def on_press(self, event):
        self.my_btn.SetLabel("New Value")


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()



RE: [wxPython] How to change button label? - deanhystad - May-31-2022

    def on_press(self, event):
        #self.SetLabel("New Value")  # Does not work because self is wxFrame, not a button
        #my_btn.SetLabel("blah") # Does not work because my_btn only existed for a brief instant inside MyFrame.__init__()
        #self.SetLabel(my_btn,"New Value")  # Does not work for many reasons.   Is trying to setLabel on a frame to a button
        self.SetLabel(self.my_btn,"New Value") # Does not work because you never assign self.my_btn a value
As Yoriz says, to set the button label, you call SetLabel() for the button.
button = wxButton(parent)
button.SetLabel("Button text goes here")
If you create the button in one method and set the label in another, you need to keep a handle to the button object. The easiest way to do this save the button object in an instance variable.
method make_stuff(self):
    panel = wx.Panel(self)  # Only use panel this one time.  No need to make instance variable
    self.button = wxButton(panel, label="Initial Button Label")  # Will use button later, so make it an instance variable

method change_stuff(self):
    self.button.SetLabel("New Button Label")  # This is the same button created in make_stuff()



RE: How to change button label? - Winfried - May-31-2022

Thanks guys. Makes sense.