以下是tkinter中message组件的常用方法和说明:
1. message.show():显示消息框,并等待用户关闭。
```python
import tkinter as tk
root = tk.Tk()
root.withdraw()
message = tk.Message(root, text="Hello World", buttons=["OK"])
message.pack()
root.mainloop()
```
2. message.config():配置消息框的属性,如标题、图标等。
```python
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
message = messagebox.showinfo("Title", "Message", icon="info")
root.mainloop()
```
3. message.add_button():添加一个按钮到消息框中。
```python
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
message = messagebox.showinfo("Title", "Message", icon="info")
message.add_button("OK", command=messagebox.destroy)
root.mainloop()
```
4. message.add_radiobutton()和message.add_optionbutton():添加单选按钮或选项按钮到消息框中。
```python
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
message = messagebox.showinfo("Title", "Message", icon="info")
message.add_radiobutton("Option 1", "1", command=messagebox.destroy)
message.add_radiobutton("Option 2", "2", command=messagebox.destroy)
message.add_optionbutton("Option A", "A", command=messagebox.destroy)
message.add_optionbutton("Option B", "B", command=messagebox.destroy)
root.mainloop()
```
5. message.add_text():添加文本框到消息框中。
```python
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
message = messagebox.showinfo("Title", "Message", icon="info")
message.add_text("Text:", "This is some text")
message.add_text("More Text:", "This is more text")
root.mainloop()
```
6. message.add_progressbar():添加进度条到消息框中。
```python
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
root = tk.Tk()
root.withdraw()
message = messagebox.showinfo("Title", "Message", icon="info")
progress = ttk.Progressbar(message, length=200)
progress.pack(pady=20)
root.mainloop()
```
7. message.add_label():添加标签到消息框中。
```python
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
root = tk.Tk()
root.withdraw()
message = messagebox.showinfo("Title", "Message", icon="info")
label = ttk.Label(message, text="This is a label")
label.pack(pady=20)
root.mainloop()
```
8. message.add_filename():添加文件对话框到消息框中。
```python
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
message = messagebox.showinfo("Title", "Message", icon="info")
file_path = filedialog.askopenfilename()
root.mainloop()
```
标签:messagebox,tkinter,tk,组件,import,message,root From: https://www.cnblogs.com/full-stack-linux-new/p/17660197.html