DEV Community

Blackmare01wolf
Blackmare01wolf

Posted on

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 (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()
Enter fullscreen mode Exit fullscreen mode

🔎 How the Code Works

Label (StaticText)

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

Textbox (TextCtrl)

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

Button

button = wx.Button(panel, label="Say Hello")
button.Bind(wx.EVT_BUTTON, self.on_button_click)
Enter fullscreen mode Exit fullscreen mode
  • 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")
Enter fullscreen mode Exit fullscreen mode

This pops up a window to display the greeting.


Layout (BoxSizer)

sizer = wx.BoxSizer(wx.VERTICAL)
Enter fullscreen mode Exit fullscreen mode
  • 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!

Top comments (0)