Quantcast
Viewing all articles
Browse latest Browse all 14215

How to add an image to tkinter?

The title pretty much says it all. I'm trying to create a windows error dialogue for a small project.

I have used this and this to create my current code. Intended OS is Windows, but I would prefer cross-platform solutions.

This is my current code:

## importsimport base64import osimport tempfileimport tkinter as tkimport zlibfrom tkinter import *from tkinter import ttk## variablesICON = zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy''sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))## main program# window setup_, ICON_PATH = tempfile.mkstemp()with open(ICON_PATH, 'wb') as icon_file:    icon_file.write(ICON)root = Tk()frm = ttk.Frame(root, padding=10)frm.grid()# window contentttk.Label(frm, text="Windows was not installed properly. Please reinstall Windows.\nError 4 (Windows error 096)").grid(column=1, row=0)ttk.Button(frm, text="Ok").grid(column=5, row=3)# window configroot.geometry("+500+500")root.title("Microsoft Windows")root.resizable(width=False, height=False)root.iconbitmap(default=ICON_PATH)widget = ttk.Label(root)widget.errorImage = tk.PhotoImage(file="""assets\err.png""")widget["image"] = widget.errorImage

When run, it creates the Tk window with text, but no image.Output:


Viewing all articles
Browse latest Browse all 14215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>