Tkinter 对话框¶
tkinter.simpledialog --- 标准 Tkinter 输入对话框¶
源码: Lib/tkinter/simpledialog.py
tkinter.simpledialog 模块包含一些便捷类和函数可用来创建简单的模态对话框以便从用户获取值。
- tkinter.simpledialog.askfloat(title, prompt, *, initialvalue=None, minvalue=None, maxvalue=None, parent=None)¶
- tkinter.simpledialog.askinteger(title, prompt, *, initialvalue=None, minvalue=None, maxvalue=None, parent=None)¶
- tkinter.simpledialog.askstring(title, prompt, *, initialvalue=None, show=None, parent=None)¶
提示用户输入所要求类型的值并将其返回,或者如果对话框被取消则返回
None。title 为对话框标题而 prompt 为显示在输入框上方的消息。 initialvalue 是放在输入框内的初始值。parent 是显示对话框的窗口。
askinteger()和askfloat()还接受 minvalue 和 maxvalue,用于设置可接受的值范围。askstring()还接受 show,用作输入文本的屏蔽字符,例如用'*'来隐藏密码。
- class tkinter.simpledialog.Dialog(parent, title=None)¶
自定义对话框的基类。 将其实例化将显示模态对话框并在用户关闭它时返回;输入的值将在
result属性中可用。- body(master)¶
可以覆盖,用于构建对话框的界面,并返回初始焦点所在的部件。
- buttonbox()¶
加入 OK 和 Cancel 按钮的默认行为。重写自定义按钮布局。
- validate()¶
Validate the data entered by the user. Return true if it is valid, in which case the dialog proceeds to
apply(); return false to keep the dialog open. The default implementation always returns true; override it to check the input.
- apply()¶
Process the data entered by the user, for example by storing it in the
resultattribute. Called aftervalidate()succeeds and just before the dialog is destroyed. The default implementation does nothing; override it to act on or store the result.
- destroy()¶
Destroy the dialog window, clearing the reference to the widget that had the initial focus.
- class tkinter.simpledialog.SimpleDialog(master, text='', buttons=[], default=None, cancel=None, title=None, class_=None)¶
A simple modal dialog that displays the message text above a row of push buttons whose labels are given by buttons, and returns the index of the button the user presses. default is the index of the button activated by the Return key, cancel the index returned when the window is closed through the window manager, title the window title, and class_ the Tk class name of the window.
- go()¶
Display the dialog, wait until the user presses a button or closes the window, and return the index of the chosen button.
tkinter.filedialog --- 文件选择对话框¶
tkinter.filedialog 模块提供了用于创建文件/目录选择窗口的类和工厂函数。
原生的载入/保存对话框¶
以下类和函数提供了文件对话窗口,这些窗口带有原生外观,具备可定制行为的配置项。这些关键字参数适用于下列类和函数:
parent —— 将对话框置于其上方的窗口title —— 窗口的标题initialdir —— 对话框的启动目录initialfile —— 打开对话框时选中的文件filetypes —— (标签,匹配模式)元组构成的列表,允许使用“*”通配符defaultextension —— 默认的扩展名,用于加到文件名后面(保存对话框)。multiple —— 为 True 则允许多选
静态工厂函数
The below functions when called create a modal, native look-and-feel dialog,
wait for the user's selection, and return it.
The exact return value depends on the function (see below); when the dialog is
cancelled it is an empty string, an empty tuple or None.
The precise type of this empty value may vary between platforms and Tk
versions, so test the result for truth rather than comparing it with a
specific value.
- tkinter.filedialog.askopenfile(mode='r', **options)¶
- tkinter.filedialog.askopenfiles(mode='r', **options)¶
Create an
Opendialog.askopenfile()returns the opened file object, orNoneif the dialog is cancelled.askopenfiles()returns a list of the opened file objects, or an empty tuple if cancelled. The files are opened in mode mode (read-only'r'by default).
- tkinter.filedialog.asksaveasfile(mode='w', **options)¶
Create a
SaveAsdialog and return the opened file object, orNoneif the dialog is cancelled. The file is opened in mode mode ('w'by default).
- tkinter.filedialog.askopenfilename(**options)¶
- tkinter.filedialog.askopenfilenames(**options)¶
Create an
Opendialog.askopenfilename()returns the selected filename as a string, or an empty string if the dialog is cancelled.askopenfilenames()returns a tuple of the selected filenames, or an empty tuple if cancelled.
- tkinter.filedialog.asksaveasfilename(**options)¶
Create a
SaveAsdialog and return the selected filename as a string, or an empty string if the dialog is cancelled.
- tkinter.filedialog.askdirectory(**options)¶
Prompt the user to select a directory, and return its path as a string, or an empty string if the dialog is cancelled. Additional keyword option: mustexist - if true, the user may only select an existing directory (false by default).
- 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)¶
退出对话框并返回当前选中项。
- ok_command()¶
Called when the user confirms the current selection. The base implementation accepts the selection and closes the dialog;
LoadFileDialogandSaveFileDialogoverride it to check the selection first.
- 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.
tkinter.dialog --- Classic Tk dialog boxes¶
Source code: Lib/tkinter/dialog.py
The tkinter.dialog module provides a simple modal dialog box built on
the classic (non-themed) Tk widgets.
- class tkinter.dialog.Dialog(master=None, cnf={}, **kw)¶
Display a modal dialog box built from the classic (non-themed) Tk widgets and wait for the user to press one of its buttons. The options, given through cnf or as keyword arguments, include title (the window title), text (the message), bitmap (an icon,
DIALOG_ICONby default), default (the index of the default button) and strings (the sequence of button labels). After construction, thenumattribute holds the index of the button the user pressed.- destroy()¶
Destroy the dialog window.
参见