2

I'm trying to create a back button. I have an image called back-button.png in the folder img.

This is my code:

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

When I run this code I get this error:

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 "."

So, why is this happening? And how can I display an image on a button?

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
leech
  • 367
  • 1
  • 4
  • 16
  • Your code is not reproducible. The error message has to do with a line 147, the contents of which don't exist at all in the code in your question. This is happening because some value returned from `CTkColorManager.single_color()` (which is not in your code; neither are `self.bg_color` or `self.appearance_mode`) is not a valid color. There is lots of code on the site for adding images to buttons; try searching around. – Sylvester Kruin Jan 06 '22 at 22:20

2 Answers2

2

The problem is that the CtkButton widget doesn't not accept parameters the same way as standard widgets. The first parameter to a CtkButton is the background color, but you're passing the root window and the root window isn't a valid color.

You need to explicitly assign the root window to the master argument.

ctk.CTkButton(master=root, image = img).pack(side = LEFT)
#             ^^^^^^^
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1

You are getting error because you are using built-in image define method which is in Tkinter not in Customtkinter. Don't confuse these two. If you want to use Customtkinter only use that because when you are making bigger projects than this, it will be a whole mess if you use these two together.

import customtkinter
from PIL import Image, ImageTk
    
window = customtkinter.CTk()
    
button_image = customtkinter.CTkImage(Image.open("assets/yourimage.png"), size=(26, 26))
    
image_button = customtkinter.CTkButton(master=window, text="Text will be gone if you don't use compound attribute",image=button_image)
image_button.pack()
    
window.mainloop()
  • 1
    Welcome to Stackoverflow! The author of the question asked why a specific error is happening, and you only post a bit of code that might resolve their problem. Please add some extra information that really helps the asker + future readers by giving insights into what is going on. – Koedlt Dec 30 '22 at 10:06