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

原文: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



猜你喜欢

转载自blog.csdn.net/polyhedronx/article/details/81981814