传统知识 |制作 TTK 样式时图像 "pyimage1" 不存在

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

我正在更新我的应用程序以使用 TTK 小部件而不是标准的 TK 小部件。但是,在尝试创建自定义样式时。

我正在使用此代码创建样式

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

但是,这会在我的 PhotoImage 中产生错误(未创建样式时不会发生这种情况)。

File "C:\Users\Realistik\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\Realistik\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

回溯中提到了 PhotoImage。它的代码是:

#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) 

如何在保留图像标签的同时修复此错误?

事实证明,要使它起作用,必须在初始化根 window 后创建样式。所以就我而言,它看起来像这样

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)