消息对话框(messagebox)
用于界面提示成功,失败,警告等相关信息提示框
语法:
tkinter.messagebox.FunctionName(title, message [, options]) 参数:
FunctionName: 这是相应的消息框函数的名称.
title: 这是在一个消息框,标题栏显示的文本.
message: 这是要显示的文字作为消息. options: 选项有替代的选择,你可以用它来定制一个标准的消息框。一些可以使用的选项是默认和家长。默认选项是用来指定默认的按钮,如中止,重试,或忽略在消息框中。父选项是用来指定要显示的消息框上的顶层窗口.
可以使用以下的功能
showinfo() 显示信息对话框
showwarning() 提示警告对话框
showerror() 显示错误对话
askquestion() 问题对话框
askokcancel() 确定还是取消对话框
askyesno() 是不是对话框
askretrycancel() 重试或者取消对话框
EG1:
from tkinter import *
from tkinter import messagebox
root = Tk()
messagebox.askokcancel("确定/取消对话框", "用刀")
messagebox.askquestion("问题对话框", "你要救么")
messagebox.askretrycancel("重启或者取消对话框", "你确定要重启么????")
messagebox.askyesno("是不是对话框","你是不是真正的快乐")
messagebox.showerror("显示错误对话框","真的很遗憾,出错啦!")
#只有一个button(确定)
messagebox.showinfo("打印信息对话框","如何能写出好的作文呢?高考作文有着许多特殊性,不能等同于平时写作。高考作文是限时作文,时间短,且教师阅卷的时间更短,因此,要注意一些应试技巧,才能得到较高分数。")
messagebox.showwarning("打印警告对话框","这里有个警告")
root.mainloop()
实行结果:
除了上述6个标准消息框外,还可以使用tkinter.messagebox._show函数创建其他类型 的消息框。tkinter.messagebox._show函数的控制参数如下。
default 指定消息框的按钮。
icon 指定消息框的图标。
message 指定消息框所显示的消息。
parent 指定消息框的父组件。
title 指定消息框的标题。
type 指定消息框的类型。
使用标准对话框(简单对话/文件对话/颜色对话)
使用 tkinter.simpledialog 模块、tkinter.filedialog 模块、 tkinter.colorchooser 模块可以创建标准的对话框。
tkinter.simpledialog模块 | 可以创建标准的输入对话框。 |
tkinter.filedialog模块 | 可以创建文件打开和保存文件对话框。 |
tkinter.colorchooser模块 | 可以创建颜色选择对话框。 |
tkinter.simpledialog模块
tkinter.simpledialog模块可以使用函数创建三种类型的对话框
tkinter.simpledialog.askstring(标题,提示文字,初始值) | 输入字符串 |
tkinter.simpledialog.askinteger(title,prompt,initialvalue) | 输入整数 |
tkinter.simpledialog.askfloat(title,prompt,initialvalue) | 输入浮点型 |
注意:第三个参数为关键字参数
tkinter.filedialog模块
tkinter.filedialog模块中可以使用两种类型的对话框
tkinter.filedialog.askopenfilename(关键字参数传入) | 创建标准的【 打开 文件】对话框。 |
tkinter.filedialog.askdirectory(关键字参数传入) | 创建标准的【 打开 文件夹】对话框。 |
tkinter.filedialog.asksaveasfilename(关键字参数传入) | 可以创建标准的【 保存 文件】对话框。 |
1.askdirectory(**options)
Ask for a directory, and return the file name
注解:在目录中只能看见文件夹,里面的文件不会在显示,返回选择的文件夹所在的绝对路径
2.askopenfile(mode='r', **options)
Ask for a filename to open, and returned the opened file
注解:返回打开的单个文件对象列表,如果单击取消则返回空列表(列表包括,IO流名称,读写模式,编码格式)
3.askopenfilename(**options)
Ask for a filename to open
注解:在目录中只能选择单个文件,然会文件的绝对路径,包括文件名和后缀名
4.askopenfilenames(**options)
Ask for multiple filenames to open
Returns a list of filenames or empty list if
cancel button selected
注解:在目录中选择多个文件,然会文件的绝对路径,包括文件名和后缀名
5.askopenfiles(mode='r', **options)
Ask for multiple filenames and return the open file
objects
returns a list of open file objects or an empty list if
cancel selected
注解:返回打开的多个文件对象列表,如果单击取消则返回空列表(列表包括,IO流名称,读写模式,编码格式)
6.asksaveasfile(mode='w', **options)
Ask for a filename to save as, and returned the opened file
7.asksaveasfilename(**options)
Ask for a filename to save as
其具有以下几个相同的可选参数
filetypes | 指定文件类型。 |
initialdir | 指定默认目录。 |
initialfile | 指定默认文件。 |
title | 指定对话框标题。 |
from tkinter import filedialog, simpledialog
from tkinter import *
root = Tk()
# 打开单个文件
def callback1():
fileName = filedialog.askopenfilename()
print(fileName)
# 打开多个文件
def callback2():
fileName = filedialog.askopenfilenames()
print(fileName)
# 打开单个文件夹
def callback3():
fileName = filedialog.askdirectory()
print(fileName)
# 打开单个文件
def callback4():
fileName = filedialog.askopenfile()
print(fileName)
# 打开多个文件
def callback5():
fileName = filedialog.askopenfiles()
print(fileName)
Button(root, text="打开单个文件", command=callback1).grid()
Button(root, text="打开多个文件", command=callback2).grid()
Button(root,text="打开文件夹",command=callback3).grid()
Button(root,text="打开文件",command=callback4).grid()
Button(root,text="打开多个文件",command=callback5).grid()
# 获取用户输入的文字
s2 = simpledialog.askstring(title="asd", prompt="请收入文字") # 生成的对象(返回值)就是string类型的,可直接使用
# 获取用户输入的数字(int型)
s = simpledialog.askinteger(title="asd", prompt="请输入int类型的数字") # 生成的对象(返回值)就是int类型的,可直接使用
print(s2)
print(s)
EG2:
from tkinter.simpledialog import *
from tkinter.filedialog import *
import tkinter
windows = tkinter.Tk()
# windows.maxsize(800,800)
windows.minsize(300, 300)
# windows.resizable(False,False)
# 窗口初始大小
# windows.geometry("1300x1300")
windows.title("place")
def openfile():
import os
tkinter.filedialog.askopenfilename(title="打开文件", initialdir=r"C:\Users\Administrator\Desktop\infinite",
filetypes=(("jpg格式", "*.jpg"), ("全部", "*.*")))
def string1():
askstring("请输入字符串", "输入字符串", initialvalue="是")
# askfloat("请输入浮点","22.22")
# a= askinteger("请输入整数","整数:1")
# print(a)
# 打开文件对话框
#
button1 = tkinter.Button(text="按钮", command=string1)
button2 = tkinter.Button(text="打开文件", command=openfile)
button2.place(x=40, y=60)
menubar = tkinter.Menu(windows)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = tkinter.Menu(menubar)
# 创建下来菜单的选项
filemenu.add_command(label="打开", command=openfile)
filemenu.add_command(label="保存")
# 创建下拉菜单的分割线
filemenu.add_separator()
filemenu.add_command(label="退出", )
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为File
menubar.add_cascade(label="文件", menu=filemenu)
# 显示总菜单
windows.config(menu=menubar)
button1.place(x=10, y=30)
windows.mainloop()
实行结果:
参考: