在运行这段代码时,我如何摆脱Python (或matplotlib)图标?即使我添加了icon='info',我仍然可以得到带有python徽标的火箭。请查阅参考照片。
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()

发布于 2022-05-14 02:48:36
icon=info用于更改消息框中的图标。这里回答得很好。
提及这一问题及其评论,一种解决方案可以是使用tk.Toplevel()创建您自己的自定义messagebox
下面是一个示例,说明您将如何对其进行编码。(您可以进一步提高对多个消息框的效率):
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()但是,如果您正在寻找的解决方案,请使用.ico图标文件更改默认图标,对于包括MessageBox 请参阅这里在内的整个Tkinter窗口,只需使用iconbitmap
import tkinter as tk
win = tk.Tk()
win.title("example")
win.iconbitmap(".ico file location")
win.mainloop()https://stackoverflow.com/questions/72232756
复制相似问题