python - TK |制作 TTK 样式时图像 "pyimage1"不存在

标签 python tkinter ttk

我正在更新我的应用程序以使用 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) 

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

最佳答案

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

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)

关于python - TK |制作 TTK 样式时图像 "pyimage1"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59507769/

相关文章:

python - 向客户端发送视频帧

python - Tkinter 按钮背景颜色在 mac os 中不起作用

python - Python 中的进度条和组合

python - TTK Notebook 在导入的选项卡之间共享数据

python - python 列表的过滤列表,如何创建最终列表?

c++ - 计算位掩码,枚举 0

python - 针对最长公共(public)子序列问题的输入大小绘制时间

python - 设置自定义轴值 pyplot

python - 检索 ttk.Treeview 项目的 'open' 选项作为 bool 值

php - PHP 和 Python 之间使用 XML-RPC 进行通信