我正在尝试创建一个后退按钮。我back-button.png
在档案夹中有一张图片img
。
这是我的代码:
from tkinter import *
import customtkinter as ctk
root = Tk()
ctk.CTkLabel(root,
text = 'This is a label',
text_font =('Verdana', 17)).pack(side = LEFT, pady = 11)
img = PhotoImage(file="./img/back-button.png")
ctk.CTkButton(root, image = img).pack(side = LEFT)
root.mainloop()
当我运行此代码时,我收到此错误:
Traceback (most recent call last):
File "c:\Users\User\Desktop\youtube-audio-downloader\tempCodeRunnerFile.py", line 11, in <module>
ctk.CTkButton(root, image = img).pack(side = LEFT)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 102, in __init__
self.draw()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 147, in draw
self.canvas.configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode))
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1646, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1636, in _configure
self.tk.call(_flatten((self._w, cmd)) self._options(cnf))
_tkinter.TclError: unknown color name "."
那么,为什么会这样呢?以及如何在按钮上显示影像?
uj5u.com热心网友回复:
问题是 CtkButton 小部件不像标准小部件那样接受自变量。CtkButton 的第一个自变量是背景颜色,但您传递的是根视窗,而根视窗不是有效颜色。
您需要将根视窗显式分配给master
自变量。
ctk.CTkButton(master=root, image = img).pack(side = LEFT)
# ^^^^^^^
0 评论