wxPython – Change labels using button

In this article we are going to learn how to make button interactive with the frame. In this article we will change the text label on the pressing button. So let’s start with the steps.

Step 1: Create a static text on the frame.
Step 2: Add button to the frame.
Step 3: Create event function for the button.
Step 4: Add code to change text label in this function.

Code :

filter_none

edit
close

play_arrow

link
brightness_4
code

# import wxPython
import wx
  
class Example(wx.Frame):
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)
  
        # function to get response after click
        def onButton(event):
            print("Button")
            st.SetLabel("GeeksforGeeks")
  
        # static text
        st = wx.StaticText(self, label ="Welcome to ")
  
        # create button
        button = wx.Button(self, wx.ID_ANY, 'Test', (10, 40))
  
        # bind onButton() function with button
        button.Bind(wx.EVT_BUTTON, onButton)
  
def main():
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
if __name__ == '__main__':
    main()

chevron_right


Output:
1. Before clicking:

2.After Clicking

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.