Tkinter messagebox弹窗
觉得有用的话,欢迎一起讨论相互学习~
![](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404123341456-2091227860.png)
![我的微博](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404125636151-1411970006.png)
![我的github](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404125603323-1670542535.jpg)
![我的B站](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404142715197-1867301511.jpg)
感谢莫烦
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x200')
def hit_me():
#tk.messagebox.showinfo(title='Hi', message='hahahaha')
#tk.messagebox.showwarning(title='Hi', message='nononono')
#tk.messagebox.showerror(title='Hi', message='No!! never')
#print(tk.messagebox.askquestion(title='Hi', message='hahahaha')) # return 'yes' , 'no'
#print(tk.messagebox.askyesno(title='Hi', message='hahahaha')) # return True, False
print(tk.messagebox.asktrycancel(title='Hi', message='hahahaha')) # return True, False
print(tk.messagebox.askokcancel(title='Hi', message='hahahaha')) # return True, False
print(tk.messagebox.askyesnocancel(title="Hi", message="haha")) # return, True, False, None
tk.Button(window, text='hit me', command=hit_me).pack()
window.mainloop()
module 'tkinter' has no attribute 'messagebox'
- 注意,这个问题是在windows系统上
python3.5.0
出现的错误。
- 解决方案是
import tkinter.messagebox
module 'tkinter.messagebox' has no attribute 'asktrycancel'
- 注意:在新版本的Tkinter中,
已经将方法asktrycancel改为askretrycancel
- 提示信息框
tk.messagebox.showinfo(title='Hi', message='hahahaha')
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200406144113380.png)
- 警告框
tk.messagebox.showwarning(title='Hi', message='nononono')
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200406144311504.png)
- 错误框
tk.messagebox.showerror(title='Hi', message='No!! never')
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200406144652734.png)
- 询问框
print(tk.messagebox.askquestion(title='Hi', message='hahahaha')) # return 'yes' , 'no'
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200406145040242.png)
- 是否框 和询问框的区别在于返回的值不同
print(tk.messagebox.askyesno(title='Hi', message='hahahaha')) # return True, False
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200406145411611.png)
- 重试框 询问是否重试
print(tk.messagebox.askretrycancel(title='Hi', message='hahahaha')) # return True, False
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200406151917274.png)
- 是否取消 三种返回值
print(tk.messagebox.askyesnocancel(title="Hi", message="haha")) # return, True, False, None
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200406152649118.png)