python tkinter-消息框、对话框、文件对话框

本文详细介绍了在Python的Tkinter库中如何使用各种消息框和对话框,包括提示、警告、错误消息框及确认对话框的创建方法。通过实例展示了不同对话框的使用场景,如文件对话框获取文件路径等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:https://www.cnblogs.com/buchizaodian/p/7076964.html

消息框 

导入

import tkinter
import tkinter.messagebox #这个是消息框,对话框的关键

 

提示消息框

tkinter.messagebox.showinfo('提示','人生苦短')

 

消息警告框

tkinter.messagebox.showwarning('警告','明日有大雨')

 

错误消息框

tkinter.messagebox.showerror('错误','出错了')

 

复制代码
import tkinter
import tkinter.messagebox
def but():
    tkinter.messagebox.showinfo('提示', '人生苦短')
    #tkinter.messagebox.showwarning('警告', '明日有大雨')
    #tkinter.messagebox.showerror('错误', '出错了')
root=tkinter.Tk()
root.title('GUI')#标题
root.geometry('800x600')#窗体大小
root.resizable(False, False)#固定窗体
tkinter.Button(root, text='hello button',command=but).pack()
root.mainloop()
复制代码

 

 对话框

 

 同样需要导入 tkinter.messagebox

复制代码
import tkinter
import tkinter.messagebox
def but():
    a=tkinter.messagebox.askokcancel('提示', '要执行此操作吗')
    print (a)
root=tkinter.Tk()
root.title('GUI')#标题
root.geometry('800x600')#窗体大小
root.resizable(False, False)#固定窗体
tkinter.Button(root, text='hello button',command=but).pack()
root.mainloop()
复制代码

 

 

点击

确定返回 Ture 

取消返回 False

 

messagebox.askquestion

a=tkinter.messagebox.askquestion('提示', '要执行此操作吗')

 

返回 yes 和 no

 

 

messagebox.askyesno

a=tkinter.messagebox.askyesno('提示', '要执行此操作吗')

返回 Ture 和 False

 

messagebox.askretrycancel

a=tkinter.messagebox.askretrycancel('提示', '要执行此操作吗')

返回 Ture 和 False

 

a=tkinter.messagebox.askyesnocancel('提示', '要执行此操作吗')

这个有三个按钮

 

是:True
否:False
取消:None

 

filedialog  文件对话框

用来获取文件路径和文件名

import tkinter.filedialog

 

a=tkinter.filedialog.askopenfilename()

输出a得到文件路径和名字

E:/1.txt



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值