- 导入tkinter包,弹窗库
import tkinter
import tkinter.messagebox
- 可执行代码
import tkinter
import tkinter.messagebox
def helloCallBack():
tkinter.messagebox.showinfo("hello python","byebye")
def main():
top = tkinter.Tk()
button = tkinter.Button(top,text = "点我",command = helloCallBack)
button.pack()
top.mainloop()
if __name__ == '__main__':
main()
- 执行结果
它让我点它我就点了它,结果……
结果它竟然跟我说拜拜,我很无奈地点了确定 - 更多(上述举例用了提示框)
提示消息框:
tkinter.messagebox.showinfo('提示','人生苦短')
消息警告框:
tkinter.messagebox.showwarning('警告','明日有大雨')
错误消息框:
tkinter.messagebox.showerror('错误','出错了')
对话框:
tkinter.messagebox.askokcancel('提示', '要执行此操作吗')#确定/取消,返回值true/false
tkinter.messagebox.askquestion('提示', '要执行此操作吗')#是/否,返回值yes/no
tkinter.messagebox.askyesno('提示', '要执行此操作吗')#是/否,返回值true/false
tkinter.messagebox.askretrycancel('提示', '要执行此操作吗')#重试/取消,返回值true/false
文件对话框:
import tkinter.filedialog
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)