Tkinter Dialogs

tkinter.simpledialog — Standard Tkinter input dialogs

Source code: Lib/tkinter/simpledialog.py


The tkinter.simpledialog module contains convenience classes and functions 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)

(typeshed)

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

class tkinter.simpledialog.Dialog(parent, title=None)
class tkinter.simpledialog.Dialog(
parent: tkinter.Misc | None,
title: str | None = None,
) → Dialog

(typeshed)

The base class for custom dialogs.

body(master)
body(master: tkinter.Frame) → tkinter.Misc | None

(typeshed)

Override to construct the dialog’s interface and return the widget that should have initial focus.

buttonbox()
buttonbox() → None

(typeshed)

Default behaviour adds OK and Cancel buttons. Override for custom button layouts.

tkinter.filedialog — File selection dialogs

Source code: Lib/tkinter/filedialog.py


The tkinter.filedialog module provides classes and factory functions for creating file/directory selection windows.

Native Load/Save Dialogs

The following classes and functions provide file dialog windows that combine a native look-and-feel with configuration options to customize behaviour. The following keyword arguments are applicable to the classes and functions listed 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) or None to the caller.

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

(typeshed)

The above two functions create an Open dialog and return the opened file object(s) in read-only mode.

tkinter.filedialog.asksaveasfile(mode='w', **options)
tkinter.filedialog.asksaveasfile(
mode: str = 'w',
*,
confirmoverwrite: bool | None = True,
defaultextension: str | None = '',
filetypes: Iterable[tuple[str, list[str] | str | tuple[str, ...]]] | None = ...,
initialdir: PathLike[bytes] | PathLike[str] | bytes | str | None = ...,
initialfile: PathLike[bytes] | PathLike[str] | bytes | str | None = ...,
parent: tkinter.Misc | None = ...,
title: str | None = ...,
typevariable: tkinter.StringVar | str | None = ...,
) → IO[Any] | None

(typeshed)

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

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

(typeshed)

The above two functions create an Open dialog and return the selected filename(s) that correspond to existing file(s).

tkinter.filedialog.asksaveasfilename(**options)
tkinter.filedialog.asksaveasfilename(
*,
confirmoverwrite: bool | None = True,
defaultextension: str | None = '',
filetypes: Iterable[tuple[str, list[str] | str | tuple[str, ...]]] | None = ...,
initialdir: PathLike[bytes] | PathLike[str] | bytes | str | None = ...,
initialfile: PathLike[bytes] | PathLike[str] | bytes | str | None = ...,
parent: tkinter.Misc | None = ...,
title: str | None = ...,
typevariable: tkinter.StringVar | str | None = ...,
) → str

(typeshed)

Create a SaveAs dialog and return the selected filename.

tkinter.filedialog.askdirectory(**options)
tkinter.filedialog.askdirectory(
*,
initialdir: PathLike[bytes] | PathLike[str] | bytes | str | None = ...,
mustexist: bool | None = False,
parent: tkinter.Misc | None = ...,
title: str | None = ...,
) → str
Prompt user to select a directory.
Additional keyword option:
mustexist - determines if selection must be an existing directory.
class tkinter.filedialog.Open(master=None, **options)
class tkinter.filedialog.SaveAs(master=None, **options)

(typeshed)

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

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.

class tkinter.filedialog.Directory(master=None, **options)
class tkinter.filedialog.Directory(
master: tkinter.Misc | None = None,
**options: Any,
) → Directory

(typeshed)

Create a dialog prompting the user to select a directory.

Note

The FileDialog class should be subclassed for custom event handling and behaviour.

class tkinter.filedialog.FileDialog(master, title=None)
class tkinter.filedialog.FileDialog(
master: tkinter.Misc,
title: str | None = None,
) → FileDialog

(typeshed)

Create a basic file selection dialog.

cancel_command(event=None)
cancel_command(event: tkinter.Event | None = None) → None

(typeshed)

Trigger the termination of the dialog window.

dirs_double_event(event)
dirs_double_event(event: tkinter.Event) → None

(typeshed)

Event handler for double-click event on directory.

dirs_select_event(event)
dirs_select_event(event: tkinter.Event) → None

(typeshed)

Event handler for click event on directory.

files_double_event(event)
files_double_event(event: tkinter.Event) → None

(typeshed)

Event handler for double-click event on file.

files_select_event(event)
files_select_event(event: tkinter.Event) → None

(typeshed)

Event handler for single-click event on file.

filter_command(event=None)
filter_command(event: tkinter.Event | None = None) → None

(typeshed)

Filter the files by directory.

get_filter()
get_filter() → tuple[str, str]

(typeshed)

Retrieve the file filter currently in use.

get_selection()
get_selection() → str

(typeshed)

Retrieve the currently selected item.

go(dir_or_file=os.curdir, pattern='*', default='', key=None)
go(
dir_or_file: tkinter.filedialog.StrPath = '.',
pattern: tkinter.filedialog.StrPath = '*',
default: tkinter.filedialog.StrPath = '',
key: Hashable | None = None,
) → Unknown

(typeshed)

Render dialog and start event loop.

ok_event(event)
ok_event(event: tkinter.Event) → None

(typeshed)

Exit dialog returning current selection.

quit(how=None)
quit(how: str | None = None) → None

(typeshed)

Exit dialog returning filename, if any.

set_filter(dir, pat)
set_filter(
dir: tkinter.filedialog.StrPath,
pat: tkinter.filedialog.StrPath,
) → None

(typeshed)

Set the file filter.

set_selection(file)
set_selection(file: tkinter.filedialog.StrPath) → None

(typeshed)

Update the current file selection to file.

class tkinter.filedialog.LoadFileDialog(master, title=None)
class tkinter.filedialog.LoadFileDialog(
master: tkinter.Misc,
title: str | None = None,
) → LoadFileDialog

(typeshed)

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

ok_command()
ok_command() → None

(typeshed)

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

class tkinter.filedialog.SaveFileDialog(master, title=None)
class tkinter.filedialog.SaveFileDialog(
master: tkinter.Misc,
title: str | None = None,
) → SaveFileDialog

(typeshed)

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

ok_command()
ok_command() → None

(typeshed)

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

tkinter.commondialog — Dialog window templates

Source code: Lib/tkinter/commondialog.py


The tkinter.commondialog module provides the Dialog class that is the base class for dialogs defined in other supporting modules.

class tkinter.commondialog.Dialog(master=None, **options)
class tkinter.commondialog.Dialog(
master: tkinter.Misc | None = None,
**options: Any,
) → Dialog

(typeshed)

show(**options)
show(**options: Any) → Any

(typeshed)

Render the Dialog window.