message box弹框代码
时间: 2023-05-28 08:03:30 浏览: 43
以下是一个基本的message box弹框代码示例:
```python
from tkinter import messagebox
import tkinter as tk
root = tk.Tk()
root.withdraw()
# 消息框的类型:showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, askretrycancel
messagebox.showinfo(title='消息', message='这是一条普通的消息')
result = messagebox.askyesno(title='询问', message='你确定要继续吗?')
if result:
messagebox.showinfo(title='确认', message='你选择了继续')
else:
messagebox.showinfo(title='取消', message='你选择了取消')
root.mainloop()
```
这段代码首先导入了`messagebox`和`tkinter`模块,然后创建了一个`tkinter`的窗口并隐藏了它。接着通过`messagebox`模块的不同方法创建了不同类型的消息框,包括:
- `showinfo`:普通的信息框。
- `showwarning`:警告框。
- `showerror`:错误框。
- `askquestion`:询问框,返回True或False。
- `askokcancel`:确认框,返回True或False。
- `askyesno`:Yes/No框,返回True或False。
- `askretrycancel`:重试/取消框,返回True或False。
在这个示例中,我们展示了如何创建一个普通消息框和一个询问框,并根据用户的选择显示不同的消息。