5

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() 
2
  • 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 Commented Apr 16, 2018 at 11:32
  • @Aemyl whoops, thanks. I was so frustrated when I didn'T get he background to work that I missed that. Commented Apr 17, 2018 at 7:07

2 Answers 2

5
  • 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() 
Sign up to request clarification or add additional context in comments.

2 Comments

A thorough and flawless answer. +1 (as soon as I have 15 rep points to give upvotes)
for anybody struggeling with "nonetype", see stackoverflow.com/questions/23231563/…
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() 

2 Comments

Great, I somehow missed that. Can you now please also tell me how to change the color of the individual widgets?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.