The second simple type of dialog box is wx.Text-EntryDialog, which is used to get short text entry from the user. Typically, you'll see this used when requesting a username or password at the beginning of a program, or as a very rudimentary replacement for a data entry form. Figure 9.3 displays a typical text dialog.
The code for this example is displayed in listing 9.3.
Listing 9.3 Code for text entry import wx if __name__ == "__main__": app = wx.PySimpleApp() dialog = wx.TextEntryDialog(None,
"What kind of text would you like to enter?", "Text Entry", "Default Value", style=wx.OK|wx.CANCEL) if dialog.ShowModal() == wx.ID_OK:
print "You entered: %s" % dialog.GetValue() dialog.Destroy()
As in the previous section, we create an instance of a dialog class, in this case wx.TextEntryDialog. The constructor for this class is a bit more complex than the simple message dialog:
wx.TextEntryDialog(parent, message, caption="Please enter text", defaultValue="", style=wx.OK | wx.CANCEL | wx.CENTRE, pos=wx.DefaultPosition)
The message argument is the text prompt that is displayed in the dialog, while the caption is displayed in the title bar. The defaultValue, if set, is displayed inside the text box. The style information can include wx.OK and wx.CANCEL, which displays the appropriate button.
Text Entry _
What krid of text would you Breto enter? pefault Value
OK I Caned
Several of the styles from an ordinary wx.TextCtrl can also be set here. The most useful would be wx.te_password, which masks the input for securely entering a password. You can also use wx.te_multiline to allow the user to enter more than one line of text in the dialog, and wx.te_left, wx.te_centre, and wx.te_right to adjust the justification of the entered text.
The last line of listing 9.3 displays another difference between the text box and the message box. The information entered by the user is stored in the dialog instance, and must be retrieved by the application afterwards. In this case, you can get at the value by using the dialog's GetValue() method. Remember, if the user presses Cancel to exit the dialog, it means they don't want you to use his entered value. You can also programmatically set the value with the Set-Value() method.
The following are convenience functions for dealing with text dialogs:
Most similar to the usage in listing 9.3 is wx.GetTextFromUser:
wx.GetTextFromUser(message, caption="input text", default_value="", parent=None)
In this example, message, caption, default_value, and parent are all in the wx.TextEntryDialog constructor. If the user presses OK, the return value of the function is the user entered string. If the user presses Cancel, the function returns the empty string.
If you want the user to enter a masked password, you can use the wx.Get-PasswordFromUser function.
wx.GetPasswordFromUser(message, caption="input text", default_value="", parent=None)
In this example, the arguments mean what you'd expect. The user input is displayed as asterisks, and the return value is as in the previous function—the string if the user hits OK, an empty string if the user hits cancel.
Finally, you can request a number from a user with the wx.GetNumberFrom-UserMethod.
wx.GetNumberFromUser(message, prompt, caption, value, min=0, max=100, parent=None)
The argument names here are a bit different. The message is an arbitrarily long message displayed above the prompt string, which is directly above the text entry field. The value argument is a numeric long, and is the default value. The min and max arguments allow you to specify a valid range for user input. If the user exits with the OK button, the method returns the entered value, converted to a long. If the value cannot be converted to a number, or the value is outside the min and max range, the function returns -1, which means that if you use this function for a range of negative numbers, you may want to consider an alternate plan.
Was this article helpful?