2017-12-01 2 views
1

ttkモジュールのウィジェットにbgの色を追加するにはどうしたらいいですか? 私は通常の方法をTkinterモジュールとして試しました。しかし、それは動作しませんでした。ttkモジュールウィジェットに色を追加する方法は?

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() 
+0

「StackOverflow」へようこそ質問をするときに少し具体的にしてください。*コード例でこれまでに試したことはありますか?*/*あなたは何を期待していますか?ヘルプについては、「[How to ask](https://stackoverflow.com/help/how-to-ask)」を参照してください。* – Hille

答えて

2

スタイルを使用してウィジェットに色を追加する必要があります。 スタイルオブジェクトを別々に定義し、ラベルにスタイルの名前を使用して必要なスタイルを取得します。私は、これはあなたが求めることを意図するものであるかどうかわからないですけれども、これは、あなたの問題を解決推測

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() 

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

は、コードを修正しました。

+0

はい、Hilleは正しいでしょう。問題をコード全体ではなく、 – Rahul

+0

次回はそれをやります。どうもありがとうございます。 – Ajak978

関連する問題