1

How to add bg color to widgets in ttk module?, The bellow isn't giving me the needed results. I tried the usual method as Tkinter module. But it didn't work.

from tkinter import *
from tkinter.ttk import *
import sqlite3

db_obj = sqlite3.connect("contact.db")
def count_index():
    cur = db_obj.cursor()
    count = cur.execute("select count(index) from contacts;")
    rowcount = cur.fetchone()[0]
    return rowcount

def enter(event=None):
    x=e1.get()
    y=e2.get()
    ci=count_index()+1
    db_obj.execute("insert into contacts(index, name, number) 
values(?,?,?);",(ci,x,y))
    db_obj.commit()


fx =Frame(bg="LightCyan2")
bt=Button(fx)
fr.pack(expand=YES)
l1=Label(fx, text="Enter name", bg="LightCyan2").grid(row=1,column=1)
l2=Label(fx, text="Enter number", bg="LightCyan2").grid(row=2,column=1)
e1=Entry(fx)
e2=Entry(fx)
e1.grid(row=1,column=2)
e2.grid(row=2,column=2)
e1.focus()
e2.focus()
bt.config(text="ENTER",command=enter)
bt.grid(row=3,column=2)
bt.bind('<Return>',enter)

fx.mainloop()
1
  • Welcome to StackOverflow, please be a bit more specific when asking question: What have you tried so far with code example? / What do you expect? / What error do you get? *For Help take a look at "How to ask" Commented Dec 1, 2017 at 8:52

1 Answer 1

2

You need to use style a style object to add color to widgets. Define the style object seperatly and the use the name of the style in your labels to get the required style.

s1 = Style()
s1.configure('My.Frame', background='LightCyan2')

Modified code:

from tkinter import *
from tkinter.ttk import *
import sqlite3

db_obj = sqlite3.connect("contact.db")
def count_index():
    cur = db_obj.cursor()
    count = cur.execute("select count(index) from contacts;")
    rowcount = cur.fetchone()[0]
    return rowcount

def enter(event=None):
    x=e1.get()
    y=e2.get()
    ci=count_index()+1
    conx.execute("insert into words(index, name, number) values(?,?,?);",(ci,x,y))
   conx.commit()


s1 = Style()
s1.configure('My.Frame', background='LightCyan2')

s2=Style()
s2.configure('My.Label', background='LightCyan2')

fx =Frame(style='My.Frame')
bt=Button(fx)
fx.pack(expand=YES)
l1=Label(fx, text="Enter word", style='My.Label').grid(row=1,column=1)
l2=Label(fx, text="Enter meaning", style='My.Label').grid(row=2,column=1)
e1=Entry(fx)
e2=Entry(fx)
e1.grid(row=1,column=2)
e2.grid(row=2,column=2)
e1.focus()
e2.focus()
bt.config(text="ENTER",command=enter)
bt.grid(row=3,column=2)
bt.bind('<Return>',enter)

fx.mainloop()

I guess this solves your problem, Though I am not sure if this is what you meant to ask.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, Hille is right, maybe you should point out the exact code lines which made the problem instead of the entire code.
I will do that next time, Thank you so much for your help.

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.