1

I have the a Tkinter App, but I'd like to change the background color to Blue of the first column

Tkinter Application

from tkinter import *
from tkinter import ttk

root = Tk()
mainframe = ttk.Frame(root, padding="3 3 500 200")
mainframe.grid(column=0, row=0)

ttk.Label(mainframe, text="COLUMN 1 ROW 1").grid(column=1, row=1)
root.mainloop()

I've tried the following, but I receive an error:

from tkinter import *
from tkinter import ttk


root = Tk()
mainframe = ttk.Frame(root, padding="3 3 500 200")
mainframe.grid(column=0, row=0)

ttk.Label(mainframe, text="COLUMN 1 ROW 1").grid(column=1, row=1,bg ="blue4")
root.mainloop() 

tkinter.TclError:

Traceback (most recent call last):
  File "/Users/chriscruz/Desktop/aesop_1.1.py", line 11, in <module>
    ttk.Label(mainframe, text="COLUMN 1 ROW 1").grid(column=1, row=1,bg ="blue4")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2029, in grid_configure
    + self._options(cnf, kw))
_tkinter.TclError: bad option "-bg": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky

2 Answers 2

3

ttk.Label doesn't have the option bg instead it has style option which you can configure like the following:

lbl = ttk.Label(mainframe, text="COLUMN 1 ROW 1")
style_ref = ttk.Style()
style_ref.configure("style_name.TLabel", background='blue')
lbl['style'] = "style_name.TLabel"
lbl.grid(column=1, row=1)

or if you were to use tkinter.Label instead:

lbl = tkinter.Label(mainframe, text="COLUMN 1 ROW 1")
lbl['bg'] = 'blue'
lbl.grid(column=1, row=1)
Sign up to request clarification or add additional context in comments.

Comments

1

The Frame class from the ttk package doesn't support the background option so try using just tkinter itself.

See a similar question here: How do I change the background of a Frame in Tkinter?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.