Python Forum
[WxPython] [SOLVED] How to change button label?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[WxPython] [SOLVED] How to change button label?
#1
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.
Reply
#2
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()
Reply
#3
    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()
Reply
#4
Thanks guys. Makes sense.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 2,796 Jan-03-2023, 06:29 PM
Last Post: Yoriz
  Can't change the colour of Tk button text Pilover 6 14,745 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,891 Aug-23-2022, 09:11 PM
Last Post: Extra
  Can't get tkinter button to change color based on changes in data dford 4 3,419 Feb-13-2022, 01:57 PM
Last Post: dford
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,853 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  [tkinter] color change for hovering over button teacher 4 8,498 Jul-04-2020, 06:33 AM
Last Post: teacher
  [Tkinter] Change Label Every 5 Seconds gw1500se 4 6,864 May-26-2020, 05:32 PM
Last Post: gw1500se
  [Tkinter] Python 3 change label text gw1500se 6 4,690 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Tkinter] Change label for multiple frames Dandy_Don 3 2,988 Apr-30-2020, 02:22 PM
Last Post: Dandy_Don
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,770 Mar-20-2020, 07:21 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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