Just a single line of text please

If you need to get a single line of text from the user, then you can use the class wx.TextEntryDialog, as follows. This snippet creates the dialog, and retrieves the entered value if the user exits with a click on the OK button:

dlg = wx.TextEntryDialog(None, "Who is buried in Grant's tomb?",

'A Question', 'Cary Grant') if dlg.ShowModal() == wx.ID_OK: response = dlg.GetValue()

Figure 2.11 shows what the dialog looks like in use.

The parameters of the wx.TextEntryDialog constructor are, in order, a parent window, the text caption inside the window, the text caption for the outside of the window (which defaults to "Please enter text"), and a default value for the user entry field (which defaults to the empty string). There is also a style argument which defaults to wx.OK | wx.cancel. You can use the style attribute to display the dialog without a cancel box, by passing only the value wx.OK. As with the wx.MessageDialog, the ShowModal() method returns the ID of the button pressed. The GetValue() method contains the value the user entered into the text field (there's a parallel SetValue() method that allows you to change the field programmatically).

Figure 2.11 A text entry dialog

What are some common errors with application objects and frames?

Was this article helpful?

0 0

Post a comment