TK | image "pyimage1" doesn't exist while making TTK styles

Question:

I am in the process of updating my app to use TTK widgets rather than the standard TK ones. However, when trying to create custom styles.

I am using this code to create a style

s = ttk.Style()
s.configure('RB.Default', foreground='grey', background=Config.BG_Colour)

However, that creates an error in my PhotoImage (that doesn’t occur when the style is not created).

File "C:UsersRealistikAppDataLocalProgramsPythonPython37libtkinter__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:UsersRealistikAppDataLocalProgramsPythonPython37libtkinter__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

The traceback mentions a the PhotoImage. The code for it is:

#Logo Image
App_Logo = PhotoImage(file="img\app_logo_small.png")
App_Logo_Label = Label(MainWindow, image=App_Logo)
App_Logo_Label.grid(row=0, column=0) 

How do I fix this error while keeping the image label?

Asked By: RealistikDash

||

Answers:

Turns out for this to work, the styles have to be created after your root window is initialised. So in my case, it looks like this

from tkinter import *  
import tkinter.ttk     # from this line you are able to make object of ttk class
MainWindow = Tk()
s = ttk.Style()
s.configure('RB.Default', foreground='grey', background=Config.BG_Colour)
Answered By: RealistikDash
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.