[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

This dialog automatically checks the input for valid floating-point syntax before it re-
turns, and it is representative of a collection of single-value input dialogs (askinteger
and askstring prompt for integer and string inputs, too). It returns the input as a
floating-point number object (not as a string) when the OK button or Enter key is
pressed, or the Python None object if the user clicks Cancel. Its two relatives return the
input as integer and string objects instead.


When the demo bar’s Open button is pressed, we get the standard file open dialog
made by calling askopenfilename and captured in Figure 8-14. This is Windows 7’s
look-and-feel; it can look radically different on Macs, Linux, and older versions of
Windows, but appropriately so.


A similar dialog for selecting a save-as filename is produced by calling asksaveasfile
name (see the Text widget section in Chapter 9 for a first example). Both file dialogs let
the user navigate through the filesystem to select a subject filename, which is returned
with its full directory pathname when Open is pressed; an empty string comes back if
Cancel is pressed instead. Both also have additional protocols not demonstrated by this
example:



  • They can be passed a filetypes keyword argument—a set of name patterns used
    to select files, which appear in the pull-down list near the bottom of the dialog.


Figure 8-12. demoDlg query, askquestion dialog


Figure 8-13. demoDlg input, askfloat dialog


432 | Chapter 8: A tkinter Tour, Part 1

Free download pdf