wxPython Buttons

In wxPython, interactive buttons are created with the wx.Button widget and events are handled by binding to wx.EVT_BUTTON.

Adding a Button and Handling Click Events

This example demonstrates creating a panel with a button and connecting a callback method when clicked:

import wx

class ButtonFrame(wx.Frame):
    def __init__(self):
        super(ButtonFrame, self).__init__(None, title="wxPython Button Example", size=(350, 220))
        panel = wx.Panel(self)
        
        btn = wx.Button(panel, label="Click Me!", pos=(110, 60))
        btn.Bind(wx.EVT_BUTTON, self.on_button_clicked)
        
        self.Centre()

    def on_button_clicked(self, event):
        wx.MessageBox("Button was clicked!", "Info", wx.OK | wx.ICON_INFORMATION)

if __name__ == '__main__':
    app = wx.App()
    frame = ButtonFrame()
    frame.Show()
    app.MainLoop()
Back ⚡ Practice with PyChallengeNext