Search code examples
pythonwxpythonuser-inputraw-input

Using wxPython to get input from user


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


Solution

  • 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