Tkinter DialogsTkinter对话框

tkinter.simpledialogStandard Tkinter input dialogs标准Tkinter输入对话框

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模块包含方便的类和函数,用于创建简单的模态对话框,以从用户处获取值。

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 value of 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 that should have initial focus.重写以构造对话框的界面并返回应该具有初始焦点的小部件。

buttonbox()

Default behaviour adds OK and Cancel buttons. 默认行为添加“确定”和“取消”按钮。Override for custom button layouts.替代自定义按钮布局。

tkinter.filedialogFile selection dialogs文件选择对话框

Source code: Lib/tkinter/filedialog.py


The tkinter.filedialog module provides classes and factory functions for creating file/directory selection windows.tkinter.filedialog模块提供用于创建文件/目录选择窗口的类和工厂函数。

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如果为true,则允许选择多个项目

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)

The above two functions create an Open dialog and return the opened file object(s) in read-only mode.以上两个函数创建一个Open对话框,并以只读模式返回打开的文件对象。

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

Create a SaveAs dialog and return a file object opened in write-only mode.创建SaveAs对话框,并返回以只读模式打开的文件对象。

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

The above two functions create an Open dialog and return the selected filename(s) that correspond to existing file(s).以上两个函数创建一个Open的对话框,并返回与现有文件相对应的选定文件名。

tkinter.filedialog.asksaveasfilename(**options)

Create a SaveAs dialog and return the selected filename.创建SaveAs对话框并返回所选文件名。

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 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.这些没有模仿平台的本机外观。

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

Create a dialog prompting the user to select a directory.创建一个对话框,提示用户选择目录。

Note

The FileDialog class should be subclassed for custom event handling and behaviour.FileDialog类应为自定义事件处理和行为的子类。

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 to file.将当前文件选择更新为file

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

A subclass of FileDialog that creates a dialog window for selecting an existing file.FileDialog的子类,用于创建用于选择现有文件的对话框窗口。

ok_command()

Test that a file is provided and that the selection indicates an already existing file.测试是否提供了文件,以及所选内容是否指示已存在的文件。

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

A subclass of FileDialog that creates a dialog window for selecting a destination file.FileDialog的子类,用于创建用于选择目标文件的对话框窗口。

ok_command()

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.commondialogDialog 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.tkinter.commondialog模块提供了Dialog类,该类是其他支持模块中定义的对话框的基类。

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

Render the Dialog window.渲染对话框窗口。

See also参阅

Modules tkinter.messagebox, Reading and Writing Files模块tkinter.messagebox,读取和写入文件