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btn_text="Ok",
title="Info", msg="", icon=None, **kwargs):
super().__init__(root, *args, **kwargs)
self.title(title)
self.resizable(False, False)
self.msg = msg
self.icon = icon
self.btn_text = str(btn_text)
# Main frame
main_frm = tk.Frame(self)
main_frm.pack(expand=True, fill=tk.BOTH, padx=(10, 10), pady=10)
# Icon
if self.icon is not None:
if isinstance(self.icon, tk.PhotoImage):
h, w = self.icon.height(), self.icon.width()
scale_w = w // 50
scale_h = h // 50
self.icon = self.icon.subsample(scale_w, scale_h)
elif isinstance(self.icon, PIL.ImageFile.ImageFile):
im = self.icon.resize((50, 50), PIL.Image.ANTIALIAS)
self.icon = PIL.ImageTk.PhotoImage(im)
else:
raise ValueError(
f"can't use {self.icon} as icon: not a photo image"
)
else:
self.icon = tk.PhotoImage(data=self.DEFAULT_ICON)
icon_label = tk.Label(main_frm, image=self.icon)
icon_label.grid(row=0, column=0, sticky=tk.NE)
# Message label
message = tk.Label(
main_frm,
text=self.msg,
anchor=tk.W,
justify=tk.LEFT,
wraplength=350,
font='Helvetica 10 bold')
message.grid(row=0, column=1, sticky=tk.NW)
main_frm.rowconfigure(0, weight=1)
# Button
ok_btn = tk.Button(
main_frm,
width=8,
text="Ok" text=self.btn_text,
command=self.on_close
)
ok_btn.bind('<KeyPress-Return>', func=self.on_close)
ok_btn.grid(row=1, column=0, columnspan=2)
# Modal dialog
self.transient(master=root)
self.grab_set()
ok_btn.focus_set()
self.deiconify()
self.protocol("WM_DELETE_WINDOW", self.on_close)
def on_close(self, *args):
self.grab_release()
self.destroy()
return "ok"
Instancia de
tkinter.PhotoImage
import tkinter as tk root = tk.Tk() root.geometry("500x500+500+500") img = tk.PhotoImage(file="/ruta/icono.gif") tk.Button( root, text="Abrir diálogo", command=lambda: InfoMessageBox( root, title="El Zen de Python", btn_text="Aceptar", msg=("Explícito es mejor que implícito.\n", icon=img ) ).place(relx=0.5, rely=0.5, anchor=tk.CENTER) root.mainloop()
Imagen cargada con PIL (
PIL.Image()
):import tkinter as tk from PIL import Image root = tk.Tk() root.geometry("500x500+500+500") img = Image.open("/ruta/incono.gif") tk.Button( root, text="Abrir diálogo", command=lambda: InfoMessageBox( root, title="El Zen de Python", btn_text="Aceptar"btn_text="Ok", msg="Explícito es mejor que implícito.\n", icon=img ) ).place(relx=0.5, rely=0.5, anchor=tk.CENTER) root.mainloop()