答案 0 :(得分:1)
您可以使用Toplevel()
创建自己的消息窗口,然后就可以执行所需的操作。
import tkinter as tk
# --- functions ---
def about():
win = tk.Toplevel()
win.title("ABOUT")
l = tk.Label(win, text="One\ntwo two\nThree Three Three", bg='white')
l.pack(ipadx=50, ipady=10, fill='both', expand=True)
b = tk.Button(win, text="OK", command=win.destroy)
b.pack(pady=10, padx=10, ipadx=20, side='right')
# --- main ---
root = tk.Tk()
b = tk.Button(root, text="About", command=about)
b.pack(fill='x', expand=True)
b = tk.Button(root, text="Close", command=root.destroy)
b.pack(fill='x', expand=True)
root.mainloop()
Linux的:
BTW:您可以找到包含留言箱代码的文件
import tkinter.messagebox
print(tkinter.messagebox.__file__)
然后在编辑器中打开以查看它是如何制作的。
编辑:您还可以创建课程MsgBox
并多次使用它。
示例显示了如何更改类中的某些元素:标签字体,按钮文本和位置
import tkinter as tk
# --- classes ---
# you can put this in separated file (it will need `import tkinter`)
import tkinter
class MsgBox(tkinter.Toplevel):
def __init__(self, title="MsgBox", message="Hello World"):
tkinter.Toplevel.__init__(self)
self.title(title)
self.label = tkinter.Label(self, text=message)
self.label['bg'] = 'white'
self.label.pack(ipadx=50, ipady=10, fill='both', expand=True)
self.button = tkinter.Button(self, text="OK")
self.button['command'] = self.destroy
self.button.pack(pady=10, padx=10, ipadx=20, side='right')
# --- functions ---
def about():
msg = MsgBox("ABOUT", "One\nTwo Two\nThree Three Three")
msg.label['font'] = 'Verdana 20 bold'
msg.button['text'] = 'Close'
msg.button.pack(side='left')
# --- main ---
root = tk.Tk()
b = tk.Button(root, text="About", command=about)
b.pack(fill='x', expand=True)
b = tk.Button(root, text="Close", command=root.destroy)
b.pack(fill='x', expand=True)
root.mainloop()
GitHub上的