messagebox消息框
语法
-
messagebox.askquestion(title,message)
:返回yes
或no
-
messagebox.askokcancel(title,message)
:返回True
或False
-
messagebox.askretrycancel(title,message)
:返回True
或False
-
messagebox.askyesno(title,message)
:返回True
或False
-
messagebox.showinfo(title,message)
:返回ok
-
messagebox.showwarning(title,message)
:返回ok
-
messagebox.showerror(title,message)
:返回ok
示例
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry("500x300")
def okcancel():
result = messagebox.askokcancel(title='askokcancel', message='这是askokcancel')
print(result)
def question():
result = messagebox.askquestion(title='askquestion', message="这是askquestion")
print(result)
def retrycancel():
result = messagebox.askretrycancel(title='askretrycancel', message="这是askretrycancel")
print(result)
def yesno():
result = messagebox.askyesno(title='askyesno', message="这是askyesno")
print(result)
def error():
result = messagebox.showerror(title='showerror', message="这是showerror")
print(result)
def warning():
result = messagebox.showwarning(title='showwarning', message="这是showwarning")
print(result)
def info():
result = messagebox.showinfo(title='showinfo', message="这是showinfo")
print(result)
button_0 = tk.Button(root, text="askokcancel", command=okcancel) # command的函数不带圆括号
button_0.pack()
button_1 = tk.Button(root, text="askquestion", command=question)
button_1.pack()
button_2 = tk.Button(root, text="askretrycancel", command=retrycancel)
button_2.pack()
button_3 = tk.Button(root, text="askyesno", command=yesno)
button_3.pack()
button_4 = tk.Button(root, text="showerror", command=error)
button_4.pack()
button_5 = tk.Button(root, text="showwarning", command=warning)
button_5.pack()
button_6 = tk.Button(root, text="showinfo", command=info)
button_6.pack()
root.mainloop()
simpledialog输入框
语法
-
simpledialog.askstring(title, prompt, initialvalue)
:必须输入字符串,initialvalue
为输入框的初始默认值 -
simpledialog.askinteger(title, prompt, initialvalue)
:必须输入整数 -
simpledialog.askfloat(title, prompt, initialvalue)
:必须输入浮点型
示例
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
root.geometry("500x300")
def askstring():
res = simpledialog.askstring(title="askstring", prompt="这是askstring", initialvalue='initialvalue')
print(res)
def askinteger():
res = simpledialog.askinteger(title="askinteger", prompt="这是askinteger")
print(res)
def askfloat():
res = simpledialog.askfloat(title="askfloat", prompt="这是askfloat")
print(res)
button_0 = tk.Button(root, text='askstring', command=askstring)
button_0.pack()
button_1 = tk.Button(root, text='askinteger', command=askinteger)
button_1.pack()
button_2 = tk.Button(root, text='askfloat', command=askfloat)
button_2.pack()
root.mainloop()
filedialog文件框
语法
filedialog.askopenfile([mode='r',title,initialdir,initialfile])
:- 打开单个文件
- 返回值:打开文件获取文件指针
filedialog.askopenfiles([mode='r',title,initialdir,initialfile])
:- 打开多个或单个文件
- 返回值:列表,包含选取的文件的指针
filedialog.askopenfilename([title,initialdir,initialfile])
- 打开单个文件
- 返回值:绝对路径。
- 适用于用户想要选择现有文件的情况。如果选择了一个不存在文件,则会出现一个弹出窗口,通知所选的文件不存在
filedialog.askopenfilenames([title,initialdir,initialfile])
:- 选择多个文件
- 返回值:元组,包含其路径
filedialog.asksaveasfilename([title,initialdir,initialfile])
:- 适应于用户想要创建新文件或替换现有文件的情况
- 如果选择现有文件,则会出现一个弹出窗口,通知所选的文件已存在,并询问是否确认替换
filedialog.askdirectory([title,initialdir,initialfile])
:- 选取文件夹(目录)
- 返回值:目录的绝对路径
参数:
title
:对话框标题initialdir
:默认初始路径initialfile
:默认初始文件
示例
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.geometry("500x300")
# 以下两个方法获取文件路径
def openfilename():
# initialdir: 默认路径
# initialfile: 默认文件
path = filedialog.askopenfilename(initialdir="D:/", initialfile='Python')
print(path)
def openfilenames():
# 参数title:设置标题
paths = filedialog.askopenfilenames(title="打开心意的文件,亲!")
print(paths)
# 以下两个方法获取文件地址的指针
def openfile():
# 参数mode默认是只读'r'的方式,不可修改
fp = filedialog.askopenfile()
print(fp)
def openfiles():
fps = filedialog.askopenfiles()
print(fps)
def directory():
dir = filedialog.askdirectory()
print(dir)
# 此函数只负责提示保存的路径,而不会真正保存文件,除非使用os模块
def saveasfilename():
path = filedialog.asksaveasfilename()
print(path)
button_0 = tk.Button(root, text="openfilename", command=openfilename)
button_0.pack()
button_1 = tk.Button(root, text="openfilenames", command=openfilenames)
button_1.pack()
button_2 = tk.Button(root, text="openfile", command=openfile)
button_2.pack()
button_3 = tk.Button(root, text="openfiles", command=openfiles)
button_3.pack()
button_4 = tk.Button(root, text="dirctory", command=directory)
button_4.pack()
button_5 = tk.Button(root, text="saveasfilename", command=saveasfilename)
button_5.pack()
root.mainloop()
colorchooser颜色选取框
语法
res = colorchooser.askcolor([color])
- 参数:
color
,默认初始选中的颜色
- 返回值:元组,选中颜色的16进制值的RGB值
- 参数:
import tkinter as tk
from tkinter import colorchooser
root = tk.Tk()
root.geometry("500x300")
def color_chose():
# color: 设置默认颜色值
res = colorchooser.askcolor(color='red')
print(res)
# 设置界面的背景颜色
root['bg'] = res[1]
button = tk.Button(root, text="color", command=color_chose)
button.pack()
root.mainloop()