Change Icon For Tkinter Messagebox

前端 未结 2 1728
说谎
说谎 2021-01-13 02:15

Is there a way to change the icon of a tkinter message box? Here is my code:

from tkinter import *
import tkinter.messagebox as messagebox

root = Tk()
mess         


        
相关标签:
2条回答
  • 2021-01-13 02:58

    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':

    root.tk.call('wm', 'iconphoto', root._w, PhotoImage(file='ico.gif'))
    

    Call this method after creating the root object and before popping the messagebox. The icon will apply to the root object as well as to the messagebox.

    0 讨论(0)
  • 2021-01-13 03:20

    Following are two possible solutions for your question:

    1. Changing the title bar icon

    Yes, we can set a custom icon for the title bar in tkinter.

    Set custom icon for title bar

    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

    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.

    Root window and message box have the same custom icon

    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

    No you can not set a custom icon for displaying inside of a message box.

    Message box icon

    But you have four preset icon options to choose from, the options are:

    • error
    • info
    • question
    • warning

    4 message box icon options

    You can use them by specifying one of the above 4 values to the icon option.

    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
    • showerror() messagebox is error
    • showwarning() messagebox is warning
    0 讨论(0)
提交回复
热议问题