本文实例讲述了Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法。分享给大家供大家参考,具体如下:
相关内容:
- messagebox
- 介绍
- 使用
- filedialog
- 介绍
- 使用
首发时间:2018-03-04 22:18
messagebox:
-
介绍:messagebox是tkinter中的消息框、对话框
-
使用:
- 导入模块:
import tkinter.messagebox
- 选择消息框的模式:
- 提示消息框:【返回”ok”】
tkinter.messagebox.showinfo(消息框标题,提示内容)
- 消息警告框【返回”ok”】:
tkinter.messagebox.showwarning(消息框标题,警告内容)
- 错误消息框【返回”ok”】:
tkinter.messagebox.showerror(消息框标题,错误提示内容)
- 对话框:
- 是\\否\\取消对话框: 【返回值:是:True 否:False 取消:None】:
tkinter.messagebox.askyesnocancel(标题,提示内容)
from tkinter import * import tkinter.messagebox def info_warn_err(): a=tkinter.messagebox.showinfo(\"我的标题\",\"我的提示1\") print(a) a=tkinter.messagebox.showwarning(\"我的标题\",\"我的提示2\") print(a) a=tkinter.messagebox.showerror(\"我的标题\", \"我的提示3\") print(a) def func2(): a=tkinter.messagebox.askyesno(\"我的标题\",\"我的提示1\") print(a) a=tkinter.messagebox.askokcancel(\"我的标题\",\"我的提示2\") print(a) a=tkinter.messagebox.askquestion(\"我的标题\",\"我的提示3\") print(a) a=tkinter.messagebox.askretrycancel(\"我的标题\",\"我的提示4\") print(a) a=tkinter.messagebox.askyesnocancel(\"我的标题\",\"我的提示5\") print(a) #这里用作演示如何使用对话框 if tkinter.messagebox.askyesno(\"我的标题\", \"确认关闭窗口吗!\"): root.destroy() root=Tk() btn=Button(root,text=\"信息、警告、错误消息框\",command=info_warn_err) btn1=Button(root,text=\"对话框\",command=func2) btn.pack() btn1.pack() root.mainloop()
filedialog:
- 介绍:filedialog是tkinter中的文件对话框
- 使用:
- 导入模块:import tkinter.filedialog
- 选择文件对话框的格式:
- tkinter.filedialog.asksaveasfilename():选择以什么文件名保存,返回文件名
- tkinter.filedialog.asksaveasfile():选择以什么文件保存,创建文件并返回文件流对象
- tkinter.filedialog.askopenfilename():选择打开什么文件,返回文件名
- tkinter.filedialog.askopenfile():选择打开什么文件,返回IO流对象
- tkinter.filedialog.askdirectory():选择目录,返回目录名
- tkinter.filedialog.askopenfilenames():选择打开多个文件,以元组形式返回多个文件名
- tkinter.filedialog.askopenfiles():选择打开多个文件,以列表形式返回多个IO流对象
import tkinter.filedialog from tkinter import * def func1(): a=tkinter.filedialog.asksaveasfilename()#返回文件名 print(a) a =tkinter.filedialog.asksaveasfile()#会创建文件 print(a) a =tkinter.filedialog.askopenfilename()#返回文件名 print(a) a =tkinter.filedialog.askopenfile()#返回文件流对象 print(a) a =tkinter.filedialog.askdirectory()#返回目录名 print(a) a =tkinter.filedialog.askopenfilenames()#可以返回多个文件名 print(a) a =tkinter.filedialog.askopenfiles()#多个文件流对象 print(a) root=Tk() btn1=Button(root,text=\"click\",command=func1) btn1.pack() root.mainloop()
- 介绍:filedialog是tkinter中的文件对话框
- 提示消息框:【返回”ok”】
- 导入模块:
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。