Using wxPython to get input from user

22,259

Solution 1

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

Solution 2

This is fairly trivial. Here is one way.

import wx
class Frame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(-1, -1))
        self.panel = wx.Panel(self)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        self.btn = wx.Button(self.panel, -1, "Name-a-matic")
        self.Bind(wx.EVT_BUTTON, self.GetName, self.btn)
        self.txt = wx.TextCtrl(self.panel, -1, size=(140,-1))
        self.txt.SetValue('name goes here')

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.btn)
        sizer.Add(self.txt)

        self.panel.SetSizer(sizer)
        self.Show()

    def GetName(self, e):

        dlg = wx.TextEntryDialog(self.panel, 'Whats yo name?:',"name-o-rama","", 
                style=wx.OK)
        dlg.ShowModal()
        self.txt.SetValue(dlg.GetValue())
        dlg.Destroy()

    def OnCloseWindow(self, e):
        self.Destroy()

app = wx.App()
frame = Frame(None, 'My Nameomatic')
app.MainLoop()

And here is another way:

import wx

class NameDialog(wx.Dialog):
    def __init__(self, parent, id=-1, title="Enter Name!"):
        wx.Dialog.__init__(self, parent, id, title, size=(-1, -1))

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.buttonSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.label = wx.StaticText(self, label="Enter Name:")
        self.field = wx.TextCtrl(self, value="", size=(300, 20))
        self.okbutton = wx.Button(self, label="OK", id=wx.ID_OK)

        self.mainSizer.Add(self.label, 0, wx.ALL, 8 )
        self.mainSizer.Add(self.field, 0, wx.ALL, 8 )

        self.buttonSizer.Add(self.okbutton, 0, wx.ALL, 8 )

        self.mainSizer.Add(self.buttonSizer, 0, wx.ALL, 0)

        self.Bind(wx.EVT_BUTTON, self.onOK, id=wx.ID_OK)
        self.Bind(wx.EVT_TEXT_ENTER, self.onOK)

        self.SetSizer(self.mainSizer)
        self.result = None

    def onOK(self, event):
        self.result = self.field.GetValue()
        self.Destroy()

    def onCancel(self, event):
        self.result = None
        self.Destroy()

class Frame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(-1, -1))
        self.panel = wx.Panel(self)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        self.btn = wx.Button(self.panel, -1, "Name-a-matic")
        self.Bind(wx.EVT_BUTTON, self.GetName, self.btn)
        self.txt = wx.TextCtrl(self.panel, -1, size=(140,-1))
        self.txt.SetValue('name goes here')

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.btn)
        sizer.Add(self.txt)

        self.panel.SetSizer(sizer)
        self.Show()

    def GetName(self, e):
        dlg = NameDialog(self)
        dlg.ShowModal()
        self.txt.SetValue(dlg.result)

    def OnCloseWindow(self, e):
        self.Destroy()

app = wx.App()
frame = Frame(None, 'My Nameomatic')
app.MainLoop()
Share:
22,259
AXO
Author by

AXO

-----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/E/ED/M/P/S/SS d(-)>-- s:>- a? C++(+++)>++++ UL++>+++ P-(--)>--- L+>++++ !E- !W+(++)@>+++ !N() !o !K- w(---)>++ !O !M->- !V->-- PS+++(+++)>+++ PE(++)>- Y++(++)>+++ PGP+(+)>+++ t+>+++$ !5+>+ X@ R>$ tv? b+>++++$ DI !D>$ G>+++ e@?>+++++$ h--(+)? !r z---* ------END GEEK CODE BLOCK------

Updated on July 09, 2022

Comments

  • AXO
    AXO almost 2 years

    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

  • Bull
    Bull over 10 years
    You need app.MainLoop() at the end of your first way
  • Dan
    Dan over 8 years
    there is no defaultValue parameter, it's just value
  • AXO
    AXO over 8 years
    I currently use Python 3 only and don't have wxPython installed anymore. But as far as I can see defaultValue is mentioned in the documentation and I'm sure that this code worked correctly at the time.
  • Dan
    Dan over 8 years
    Must be one of the Phoenix changes
  • Raymond Yee
    Raymond Yee about 6 years
    For Phoenix, change defaultValue= default_value to value= default_value