Tkinter 对话框¶
tkinter.simpledialog --- Standard Tkinter input dialogs¶
源码: 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.filedialog --- File selection dialogs¶
The tkinter.filedialog module provides classes and factory functions for
creating file/directory selection windows.
原生的载入/保存对话框。¶
以下类和函数提供了文件对话窗口,这些窗口带有原生外观,具备可定制行为的配置项。这些关键字参数适用于下列类和函数:
parent —— 将对话框置于其上方的窗口title —— 窗口的标题initialdir —— 对话框的启动目录initialfile —— 打开对话框时选中的文件filetypes —— (标签,匹配模式)元组构成的列表,允许使用“*”通配符defaultextension —— 默认的扩展名,用于加到文件名后面(保存对话框)。multiple —— 为 True 则允许多选
静态工厂函数
调用以下函数时,会创建一个模态的、原生外观的对话框,等待用户选取,然后将选中值或 None 返回给调用者。
- tkinter.filedialog.askopenfile(mode='r', **options)¶
- tkinter.filedialog.askopenfiles(mode='r', **options)¶
上述两个函数创建了
Open对话框,并返回一个只读模式打开的文件对象。
- tkinter.filedialog.askopenfilename(**options)¶
- tkinter.filedialog.askopenfilenames(**options)¶
以上两个函数创建了
Open对话框,并返回选中的文件名,对应着已存在的文件。
- tkinter.filedialog.askdirectory(**options)¶
- 提示用户选择一个目录。其他关键字参数:mustexist —— 确定是否必须为已存在的目录。
- class tkinter.filedialog.Open(master=None, **options)¶
- class tkinter.filedialog.SaveAs(master=None, **options)¶
上述两个类提供了用于保存和加载文件的原生对话窗口。
便捷类
以下类用于从头开始创建文件/目录窗口。不会模仿当前系统的原生外观。
- class tkinter.filedialog.Directory(master=None, **options)¶
创建对话框,提示用户选择一个目录。
备注
为了实现自定义的事件处理和行为,应继承 FileDialog 类。
- class tkinter.filedialog.FileDialog(master, title=None)¶
创建一个简单的文件选择对话框。
- cancel_command(event=None)¶
触发对话窗口的终止。
- dirs_double_event(event)¶
目录双击事件的处理程序。
- dirs_select_event(event)¶
目录单击事件的处理程序。
- files_double_event(event)¶
文件双击事件的处理程序。
- files_select_event(event)¶
文件单击事件的处理程序。
- filter_command(event=None)¶
按目录筛选文件。
- get_filter()¶
获取当前使用的文件筛选器。
- get_selection()¶
获取当前选中项。
- go(dir_or_file=os.curdir, pattern='*', default='', key=None)¶
显示对话框并启动事件循环。
- ok_event(event)¶
退出对话框并返回当前选中项。
- quit(how=None)¶
退出对话框并返回文件名。
- set_filter(dir, pat)¶
设置文件筛选器。
- set_selection(file)¶
将当前选中文件更新为 file。
tkinter.commondialog --- Dialog window templates¶
源码: Lib/tkinter/commondialog.py
The tkinter.commondialog module provides the Dialog class that
is the base class for dialogs defined in other supporting modules.
参见