'Tkinter get rid of the python icon in messagebox tkinter
How can I get rid of the Python (or matplotlib
) icon when running this code? Even when I add icon='info'
, I still get the rocket with the python logo. Please check the photo in reference.
from tkinter import * import tkinter as tk from tkinter import messagebox as tm
root=Tk() root.geometry("1200x1200") canvas1 = tk.Canvas(root, width = 300, height = 300) canvas1.pack() def ExitApplication():
text=text= ' Our team thank you for your visit ! '
MsgBox=tk.messagebox.askquestion('Exit the platform' , text, icon='info')
if MsgBox=='yes':
root.destroy()
else:
tk.messagebox.showinfo('Return', 'You will now return to the application screen ', icon='info')
exit_button = Button(root, text="Exit ", command=ExitApplication) canvas1.create_window(200, 200, window=exit_button)
root.mainloop()
Solution 1:[1]
icon=info
is used for changing the icon inside message box.
Answered here nicely.
Referring to this question and its comments, one solution could be to create your own custom messagebox using tk.Toplevel()
:
Here is an example of how you would be coding it. (You can further make it more efficient for multiple messageboxes):
from tkinter import *
import tkinter as tk
#from tkinter import messagebox as tm
root=Tk()
root.geometry("1200x1200")
canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()
def ExitApplication():
text = ' Our team thank you for your visit ! '
MsgBox=Toplevel(root)
MsgBox.title("Exit the platform")
MsgBox.geometry(f"300x100+{root.winfo_x()}+{root.winfo_y()}")
icon = PhotoImage(file="Any image file")#provide here the image file location
MsgBox.iconphoto(True, icon)
l1=Label(MsgBox, image="::tk::icons::question")
l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")
l2=Label(MsgBox,text=text)
l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")
b1=Button(MsgBox,text="Yes",command=root.destroy,width = 10)
b1.grid(row=1, column=1, padx=(2, 35), sticky="e")
b2=Button(MsgBox,text="No",command=lambda:[MsgBox.destroy(), returnBack()],width = 10)
b2.grid(row=1, column=2, padx=(2, 35), sticky="e")
def returnBack():
pass
#Similarly, create a custom messagebox using Toplevel() for showing the following info:
#tk.messagebox.showinfo('Return', 'You will now return to the application screen ', icon='info')
exit_button = Button(root, text="Exit ", command=ExitApplication)
canvas1.create_window(200, 200, window=exit_button)
root.mainloop()
But if you are looking for a solution to change the default icon with an .ico icon file, for entire Tkinter windows including MessageBox refer here, just use iconbitmap
:
import tkinter as tk
win = tk.Tk()
win.title("example")
win.iconbitmap(".ico file location")
win.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kartikeya |