3

I'm growing desperate with this simple tkinter program: I can't seem to be able to change the background color (or the color of the individual widgets)! What is going wrong here?

Below is a gist of the various attempts that I and the errors that I received

import tkinter
import tkinter.ttk as tk

root = tkinter.Tk()

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

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

#root.config(background="black")    # does nothing
#frame.config(background="black")   # Error: unknown option "-background"
#lab.config(background="black")     # Error: 'NoneType' object has no attribute 'config'

root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
user7088941
  • 261
  • 1
  • 4
  • 11
  • The last error is really obvious: you assign the return value of `tk.Label().grid()` to `lab`. You have to assign the return value of `tk.Label()` to `lab` and call `.grid()` after the assignment – Aemyl Apr 16 '18 at 11:32
  • @Aemyl whoops, thanks. I was so frustrated when I didn'T get he background to work that I missed that. – user7088941 Apr 17 '18 at 07:07

2 Answers2

4
  • frame.config(background="black") returns the error unknown option "-background" because this is a ttk.Frame, not a tkinter.Frame and the background of a ttk.Frame is changed using a ttk.Style (see example below).

  • lab.config(background="black") gives the error 'NoneType' object has no attribute 'config' because you did lab = tk.Label(...).grid(...) and grid returns None so lab is None, not a Label.

  • root.config(background="black") does nothing because the widgets fill the window so you cannot see the background like naknak12 explained.

Here is an example using ttk widgets:

import tkinter
from tkinter import ttk

root = tkinter.Tk()
root.configure(background='black')
# style configuration
style = ttk.Style(root)
style.configure('TLabel', background='black', foreground='white')
style.configure('TFrame', background='black')

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

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


root.mainloop()

And an example without ttk:

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()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • 1
    A thorough and flawless answer. +1 (as soon as I have 15 rep points to give upvotes) – user7088941 Apr 17 '18 at 12:07
  • for anybody struggeling with "nonetype", see https://stackoverflow.com/questions/23231563/nonetype-object-has-no-attribute-config – clockw0rk Jan 17 '19 at 10:53
1

if you use root.config(background="black") and your resize the window, you can see the black background.

import tkinter
import tkinter.ttk as tk
from tkinter import * 

root = tkinter.Tk()

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

Button(frame, text="Open file", command=None).grid(column=0, row=1 )
Label(frame, bg='black', fg="white", text="test test test test test test ").grid(column=0, row=2 )

root.config(background="blue")
root.mainloop()
naknak12
  • 41
  • 10
  • Great, I somehow missed that. Can you now please also tell me how to change the color of the individual widgets? – user7088941 Apr 16 '18 at 10:37
  • Thanks, +1 (as soon as I have 15 rep points to give upvotes) . Though fixing the color on the side of the button needs an additional command, as I saw from the other answer. – user7088941 Apr 17 '18 at 12:08