Tkinter 對話框

tkinter.simpledialog --- 標準 Tkinter 輸入對話框

原始碼:Lib/tkinter/simpledialog.py


Thetkinter.simpledialog module contains convenience classes andfunctions for creating simple modal dialogs to get a value from the user.

tkinter.simpledialog.askfloat(title,prompt,**kw)
tkinter.simpledialog.askinteger(title,prompt,**kw)
tkinter.simpledialog.askstring(title,prompt,**kw)

The above three functions provide dialogs that prompt the user to enter a valueof the desired type.

classtkinter.simpledialog.Dialog(parent,title=None)

The base class for custom dialogs.

body(master)

Override to construct the dialog's interface and return the widget thatshould have initial focus.

buttonbox()

Default behaviour adds OK and Cancel buttons. Override for custom buttonlayouts.

tkinter.filedialog --- File selection dialogs

原始碼:Lib/tkinter/filedialog.py


Thetkinter.filedialog module provides classes and factory functions forcreating file/directory selection windows.

Native Load/Save Dialogs

The following classes and functions provide file dialog windows that combine anative look-and-feel with configuration options to customize behaviour.The following keyword arguments are applicable to the classes and functionslisted below:

parent - the window to place the dialog on top of
title - the title of the window
initialdir - the directory that the dialog starts in
initialfile - the file selected upon opening of the dialog
filetypes - a sequence of (label, pattern) tuples, '*' wildcard is allowed
defaultextension - default extension to append to file (save dialogs)
multiple - when true, selection of multiple items is allowed

Static factory functions

The below functions when called create a modal, native look-and-feel dialog,wait for the user's selection, then return the selected value(s) orNone to thecaller.

tkinter.filedialog.askopenfile(mode='r',**options)
tkinter.filedialog.askopenfiles(mode='r',**options)

The above two functions create anOpen dialog and return the openedfile object(s) in read-only mode.

tkinter.filedialog.asksaveasfile(mode='w',**options)

Create aSaveAs dialog and return a file object opened in write-only mode.

tkinter.filedialog.askopenfilename(**options)
tkinter.filedialog.askopenfilenames(**options)

The above two functions create anOpen dialog and return theselected filename(s) that correspond to existing file(s).

tkinter.filedialog.asksaveasfilename(**options)

Create aSaveAs dialog and return the selected filename.

tkinter.filedialog.askdirectory(**options)
Prompt user to select a directory.
Additional keyword option:
mustexist - determines if selection must be an existing directory.
classtkinter.filedialog.Open(master=None,**options)
classtkinter.filedialog.SaveAs(master=None,**options)

The above two classes provide native dialog windows for saving and loadingfiles.

Convenience classes

The below classes are used for creating file/directory windows from scratch.These do not emulate the native look-and-feel of the platform.

classtkinter.filedialog.Directory(master=None,**options)

Create a dialog prompting the user to select a directory.

備註

TheFileDialog class should be subclassed for custom eventhandling and behaviour.

classtkinter.filedialog.FileDialog(master,title=None)

Create a basic file selection dialog.

cancel_command(event=None)

Trigger the termination of the dialog window.

dirs_double_event(event)

Event handler for double-click event on directory.

dirs_select_event(event)

Event handler for click event on directory.

files_double_event(event)

Event handler for double-click event on file.

files_select_event(event)

Event handler for single-click event on file.

filter_command(event=None)

Filter the files by directory.

get_filter()

Retrieve the file filter currently in use.

get_selection()

Retrieve the currently selected item.

go(dir_or_file=os.curdir,pattern='*',default='',key=None)

Render dialog and start event loop.

ok_event(event)

Exit dialog returning current selection.

quit(how=None)

Exit dialog returning filename, if any.

set_filter(dir,pat)

Set the file filter.

set_selection(file)

Update the current file selection tofile.

classtkinter.filedialog.LoadFileDialog(master,title=None)

A subclass of FileDialog that creates a dialog window for selecting anexisting file.

ok_command()

Test that a file is provided and that the selection indicates analready existing file.

classtkinter.filedialog.SaveFileDialog(master,title=None)

A subclass of FileDialog that creates a dialog window for selecting adestination file.

ok_command()

Test whether or not the selection points to a valid file that is not adirectory. Confirmation is required if an already existing file isselected.

tkinter.commondialog --- Dialog window templates

原始碼:Lib/tkinter/commondialog.py


Thetkinter.commondialog module provides theDialog class thatis the base class for dialogs defined in other supporting modules.

classtkinter.commondialog.Dialog(master=None,**options)
show(color=None,**options)

Render the Dialog window.