Search code examples
pythonpython-3.xtkinterbackgroundbackground-color

How to change my background color with tkinter?


Basically i'm trying to change the background color of this code but it doesn't do anything (it doesn't throw an error but the background color does not change), I've tried a lot of different things but it doesn't do anything, what do i need to do or what is the problem here?

I have try several different commands but nothing seems to work.

The funny thing is that I did a little test code to see if this was a typo or something, and in my test code the background color DOES change, i don't know why it doesn't work in the main code, TEST CODE:

    import tkinter

    root = tkinter.Tk()

    frame = tkinter.Frame(root)
    frame.grid(column=0, row=0)

    tkinter.Button(frame,text="Open file",command=None).grid(column=0, row=1 )
    lab = tkinter.Label(frame, text="test test test test test test ")
    lab.grid(column=0, row=2)

    root.configure(background='black')
    lab.configure(background='black', foreground='white')
    frame.configure(background='black')

    root.mainloop()

MAIN CODE:

import tkinter as tk
from PIL import ImageTk, Image
import os
import requests
from io import BytesIO

root = tk.Tk()
root.title('PRUEBAAAAAASASASASA')
root.configure(bg='#00ff00')
img_url = "http://atlanticschools.net/wp-content/uploads/2017/05/PISA_LOGO-04.png"
response = requests.get(img_url)
img_data = response.content
img = ImageTk.PhotoImage(Image.open(BytesIO(img_data)))
panel = tk.Label(root, image=img)

panel.pack(side="bottom", fill="both", expand="yes")

root.configure(background='black')
root.mainloop()

The output from the main code is just the image and a default background and the output from the test code does have a changed background color


Solution

  • Your panel label is taking all the space of the root window. So to change the bg color, config its background colour instead.

    panel = tk.Label(root, image=img, bg="black")