脚本专栏 发布日期:2025/11/8 浏览次数:1
本文实例讲述了Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法。分享给大家供大家参考,具体如下:
首发时间:2018-03-04 22:18
import tkinter.messagebox
tkinter.messagebox.showinfo(消息框标题,提示内容)
tkinter.messagebox.showwarning(消息框标题,警告内容)
tkinter.messagebox.showerror(消息框标题,错误提示内容)
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()

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()
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。