[英]Change Icon For Tkinter Messagebox
Is there a way to change the icon of a tkinter message box?有没有办法改变 tkinter 消息框的图标? Here is my code:
这是我的代码:
from tkinter import *
import tkinter.messagebox as messagebox
root = Tk()
messagebox.showinfo(title='Example',message='This is an example')
root.mainloop()
Is there an option to change the icon from the default tkinter feather to a custom ico?是否可以将图标从默认的 tkinter 羽毛更改为自定义 ico?
Following are two possible solutions for your question:以下是您的问题的两种可能的解决方案:
1. Changing the title bar icon 1.更改标题栏图标
Yes, we can set a custom icon for the title bar in tkinter.是的,我们可以在 tkinter 中为标题栏设置自定义图标。
Code:代码:
import tkinter as tk
window = tk.Tk()
# change title bar icon
window.iconbitmap('book_2.ico')
window.mainloop()
NOTE: Use .ico files with the iconbitmap()
function注意:使用带有
iconbitmap()
函数的.ico文件
If you set a custom icon for the root window, then this same icon will be set as the title bar icon for all other child window(s) including the message boxes from messagebox module.如果您为根窗口设置自定义图标,则该图标将被设置为所有其他子窗口的标题栏图标,包括来自消息框模块的消息框。
Code:代码:
import tkinter as tk
import tkinter.messagebox as tkmb
window = tk.Tk()
# change title bar icon
window.iconbitmap('book_2.ico')
# same icon is also set for the message box
tkmb.showinfo(title='Info', message='Info message box')
window.mainloop()
2. Changing the icon inside message box 2.更改消息框内的图标
No you can not set a custom icon for displaying inside of a message box.不,您不能设置自定义图标以在消息框内显示。
But you have four preset icon options to choose from, the options are:但是您有四个预设图标选项可供选择,选项包括:
You can use them by specifying one of the above 4 values to the icon option.您可以通过为icon选项指定上述 4 个值之一来使用它们。
import tkinter.messagebox as tkmb
tkmb.showinfo(title='Message Box', message='Error message', icon='error')
tkmb.showinfo(title='Message Box', message='Info message', icon='info')
tkmb.showinfo(title='Message Box', message='Question message', icon='question')
tkmb.showinfo(title='Message Box', message='Warning message', icon='warning')
NOTE: The default icon for a注意:默认图标
showinfo()
messagebox is info showinfo()
消息框是信息showerror()
messagebox is error showerror()
消息框出错showwarning()
messagebox is warning showwarning()
消息框是警告Yes, there is such an option.是的,有这样的选择。 Assuming your root Tkinter instance is called
root
, your import statement is from tkinter import *
, and your image file is named 'ico.gif'
:假设您的根 Tkinter 实例称为
root
,您的导入语句from tkinter import *
,并且您的图像文件名为'ico.gif'
:
root.tk.call('wm', 'iconphoto', root._w, PhotoImage(file='ico.gif'))
Call this method after creating the root
object and before popping the messagebox
.在创建
root
对象之后和弹出messagebox
之前调用此方法。 The icon will apply to the root object as well as to the messagebox
.该图标将应用于根对象以及
messagebox
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.