wxPython input dialog


Input dialogs let your user give you feedback or input. They appear in desktop applications once in a while.

wxPython supports input dialogs, they are included with the framework.
A typical wxPython dialog may look like:

wx input
input dialog made with wxPython

wxPython input dialog

The example code below creates an input dialog with wxPython:

#!/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()

A wxPython textbox can be added to a window using the function:

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

Where the first argument is the frame, the second argument is the label and the last argument is the window title.

The function below displays the dialog and waits for a user to press one of the buttons:

dlg.ShowModal()

You can get the button pressed by picking one of these:

wx.OK
wx.CANCEL

(the result is either one of these)

After input is been given, you can get the input text using dlg.GetValue() function.