0

when I use this code to change the color of the window and the label, everything goes great '

import tkinter
from tkinter.colorchooser import askcolor

photo = tkinter.PhotoImage(file="something.png")
label = tkinter.Label(
    window,
    text="this is a label",
    image=photo,
    compound='bottom'
)

label.pack(ipadx=0, ipady=0)

def change_color():
    colors = askcolor(title="Tkinter Color Chooser")
    window.configure(bg=colors[1])
    label.configure(bg=colors[1])

ttk = tkinter.Button(
    window,
    text='Select a Color',
    command=change_color).pack(expand=True)

but if I add the .pack() to the first label declaration, and I try to change the color it returns error

label = tkinter.Label(
        window,
        text="this is a label",
        image=photo,
        compound='bottom'
).pack(ipadx=0, ipady=0)
TheLizzard
  • 7,248
  • 2
  • 11
  • 31

1 Answers1

0

A common mistake by beginners. Move the .pack() onto a separate line because you have to assign the variable to the label object and not to the .pack method which return None. So do this -

label = tkinter.Label(
        window,
        text="this is a label",
        image=photo,
        compound='bottom'
    )
label.pack(ipadx=0, ipady=0)
PCM
  • 2,881
  • 2
  • 8
  • 30