Getting Started with wxPython: Labels, Buttons, and Textboxes

we’ll learn how to create a simple GUI application using wxPython. This library lets you build desktop applications with Python that look native on Windows, macOS, and Linux.

In this post, we’ll cover:

✅ How to create a window
✅ How to add a Label (S…


This content originally appeared on DEV Community and was authored by Blackmare01wolf

we’ll learn how to create a simple GUI application using wxPython. This library lets you build desktop applications with Python that look native on Windows, macOS, and Linux.

In this post, we’ll cover:

✅ How to create a window

✅ How to add a Label (StaticText)

✅ How to add a Textbox (TextCtrl)

✅ How to add a Button and handle a click event

✅ A simple layout using BoxSizer

Let’s get coding!

🐍 Full Code Example

Here’s a minimal wxPython app with a label, textbox, and button:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title="My wxPython App", size=(300, 200))

        panel = wx.Panel(self)

        # Create a vertical BoxSizer to arrange widgets vertically
        sizer = wx.BoxSizer(wx.VERTICAL)

        # 1. Label
        label = wx.StaticText(panel, label="Enter your name:")
        sizer.Add(label, flag=wx.ALL, border=10)

        # 2. Textbox
        self.textbox = wx.TextCtrl(panel)
        sizer.Add(self.textbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)

        # 3. Button
        button = wx.Button(panel, label="Say Hello")
        button.Bind(wx.EVT_BUTTON, self.on_button_click)
        sizer.Add(button, flag=wx.ALL|wx.ALIGN_CENTER, border=10)

        panel.SetSizer(sizer)

        self.Centre()
        self.Show()

    def on_button_click(self, event):
        name = self.textbox.GetValue()
        if name:
            wx.MessageBox(f"Hello, {name}!", "Greetings")
        else:
            wx.MessageBox("Please enter your name.", "Warning")

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

🔎 How the Code Works

Label (StaticText)

label = wx.StaticText(panel, label="Enter your name:")
  • Creates simple text in your window.
  • Use it to show instructions or labels for input fields.

Textbox (TextCtrl)

self.textbox = wx.TextCtrl(panel)
  • Lets the user type text.
  • We read the text later using:
  name = self.textbox.GetValue()

Button

button = wx.Button(panel, label="Say Hello")
button.Bind(wx.EVT_BUTTON, self.on_button_click)
  • Creates a clickable button.
  • We connect it to a function (on_button_click) that runs when the user clicks it.

MessageBox

When the user clicks the button, we show a message box:

wx.MessageBox(f"Hello, {name}!", "Greetings")

This pops up a window to display the greeting.

Layout (BoxSizer)

sizer = wx.BoxSizer(wx.VERTICAL)
  • Helps arrange widgets vertically or horizontally.
  • We add widgets to the sizer to keep the layout organized.

✅ How to Run It

  1. Install wxPython:

    pip install wxPython
    
  2. Save the code as, e.g.:

    my_wx_app.py
    
  3. Run it:

    python my_wx_app.py
    

Enjoy your first wxPython app!

Let me know what else you’d like to add or customize for this post. Happy coding!


This content originally appeared on DEV Community and was authored by Blackmare01wolf


Print Share Comment Cite Upload Translate Updates
APA

Blackmare01wolf | Sciencx (2025-06-27T05:45:19+00:00) Getting Started with wxPython: Labels, Buttons, and Textboxes. Retrieved from https://www.scien.cx/2025/06/27/getting-started-with-wxpython-labels-buttons-and-textboxes/

MLA
" » Getting Started with wxPython: Labels, Buttons, and Textboxes." Blackmare01wolf | Sciencx - Friday June 27, 2025, https://www.scien.cx/2025/06/27/getting-started-with-wxpython-labels-buttons-and-textboxes/
HARVARD
Blackmare01wolf | Sciencx Friday June 27, 2025 » Getting Started with wxPython: Labels, Buttons, and Textboxes., viewed ,<https://www.scien.cx/2025/06/27/getting-started-with-wxpython-labels-buttons-and-textboxes/>
VANCOUVER
Blackmare01wolf | Sciencx - » Getting Started with wxPython: Labels, Buttons, and Textboxes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/27/getting-started-with-wxpython-labels-buttons-and-textboxes/
CHICAGO
" » Getting Started with wxPython: Labels, Buttons, and Textboxes." Blackmare01wolf | Sciencx - Accessed . https://www.scien.cx/2025/06/27/getting-started-with-wxpython-labels-buttons-and-textboxes/
IEEE
" » Getting Started with wxPython: Labels, Buttons, and Textboxes." Blackmare01wolf | Sciencx [Online]. Available: https://www.scien.cx/2025/06/27/getting-started-with-wxpython-labels-buttons-and-textboxes/. [Accessed: ]
rf:citation
» Getting Started with wxPython: Labels, Buttons, and Textboxes | Blackmare01wolf | Sciencx | https://www.scien.cx/2025/06/27/getting-started-with-wxpython-labels-buttons-and-textboxes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.