Chris
Chris

Reputation: 5804

How to change the background color of a TKinter Column?

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

Upvotes: 1

Views: 9618

Answers (2)

Nae
Nae

Reputation: 15345

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)

Upvotes: 3

Autio
Autio

Reputation: 327

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?

Upvotes: 1

Related Questions