python logo

python dialog box input


Python hosting: Host, run, and code Python in the cloud!

If you’re developing desktop applications with Python, you’ll likely encounter scenarios where you need user input. Enter the input dialog - an efficient way for users to provide feedback or input to your app.
wxPython, a popular GUI toolkit for the Python programming language, offers in-built support for input dialogs. This article guides you through creating a typical wxPython input dialog, accompanied by an image for clarity.

wxPython input dialog

Related course: Creating GUI Applications with wxPython

How to Create an Input Dialog in wxPython

To set up an input dialog using wxPython, follow the example code provided below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
import wx

def onButton(event):
print "Button pressed."

app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,200,50)
# Create text input
dlg = wx.TextEntryDialog(frame, 'Enter some text','Text Entry')
dlg.SetValue("Default")
if dlg.ShowModal() == wx.ID_OK:
print('You entered: %s\n' % dlg.GetValue())
dlg.Destroy()

As observed, a wxPython textbox can easily be integrated into a window. It can be added using:

1
wx.TextEntryDialog(frame, 'Enter some text','Text Entry')

Here, the first argument signifies the frame, the second stands for the label, and the last one denotes the window title.

To display the dialog and wait for the user’s response (i.e., button press), use:

1
dlg.ShowModal()

Post the user’s response, you can discern which button was pressed using the constants:

1
2
wx.OK
wx.CANCEL

Eventually, after the user provides their input, retrieve the entered text with the dlg.GetValue() function.

Related course: Creating GUI Applications with wxPython

← Previous | Next →






Leave a Reply: