Using wxPython to get input from user

AXO picture AXO · Aug 30, 2013 · Viewed 21.2k times · Source

Suppose I need to replace the raw_input function in the following code with a wxPython dialog box that asks for user input and returns the value to program:

...
x = raw_input("What's your name?")
print 'Your name was', x
...

I'm just looking for a simple way to do that. Thanks

Answer

AXO picture AXO · Oct 4, 2013

Here is another simple way that does what I was looking for:

import wx

def ask(parent=None, message='', default_value=''):
    dlg = wx.TextEntryDialog(parent, message, defaultValue=default_value)
    dlg.ShowModal()
    result = dlg.GetValue()
    dlg.Destroy()
    return result

# Initialize wx App
app = wx.App()
app.MainLoop()

# Call Dialog
x = ask(message = 'What is your name?')
print 'Your name was', x