首页 > 解决方案 > 这些文件位于何处?

问题描述

所以,在这个问题中: Change Icon For Tkinter Messagebox

有人提到 tkinter 消息框中的默认图标:“无法更改”。那么有人知道这张图片的位置,以便我可以将其更改为我自己的图片

标签: pythontkinterpython-3.8

解决方案


制作自己的消息框非常容易。比尝试更改该图标要容易得多。在这里,我会让你开始:

import tkinter as tk
from tkinter import ttk

class Popup(tk.Toplevel):
    def __init__(self, title='', message='', master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.title(title)

        lbl = tk.Label(self, text=message, font=('bold', 14))
        lbl.pack()

        btn = ttk.Button(self, text="OK", command=self.destroy)
        btn.pack()

        # The following commands keep the popup on top. Must be at the end of __init__
        self.transient(self.master) # set to be on top of the main window
        self.grab_set() # hijack all commands from the master (clicks on the main window are ignored)
        self.master.wait_window(self) # pause anything on the main window until this one closes

### TEST / DEMO CODE:

def show_custom_message():
    Popup("Title", "This is the popup message")

def main():
    root = tk.Tk()
    lbl = tk.Label(text="this is the main window")
    lbl.pack()

    btn = tk.Button(text='click me', command=show_custom_message)
    btn.pack()

    root.mainloop()

if __name__ == '__main__':
    main()

剩下的就是以您想要的任何方式对弹出窗口进行样式化。


推荐阅读