2017-09-03 7 views
0

私はこのプログラムに取り組んできましたが、何が間違っているかを知ることは非常に難しいと感じています。私はかなり新しいので、これはかなりマイナーなことがあります。tkinterのエントリーボックスの背景色を変更する

チェックボックスをオンにすると、入力ボックスの背景色を変更するプログラムを取得しようとしています。それとも、もし何とか私がそれを動的に変えることができれば、それはさらに良くなるでしょう。助けのための

TodayReading = [] 
colour = "" 
colourselection= ['green3', 'dark orange', "red3"] 
count = 0 

def MakeForm(root, fields): 
    entries = [] 
    for field in fields: 
     row = Frame(root) 
     lab = Label(row, width=15, text=field, font=("Device",10, "bold"), anchor='center') 
     ent = Entry(row) 
     row.pack(side=TOP, padx=5, fill=X, pady=5) 
     lab.pack(side=LEFT) 
     ent.pack(side=RIGHT, expand=YES, fill=X) 
     entries.append((field, ent)) 
    return entries 

def SaveData(entries): 
    import time 
    for entry in entries: 
     raw_data_point = entry[1].get() 
     data_point = (str(raw_data_point)) 
     TodayReading.append(data_point) 
    c.execute("CREATE TABLE IF NOT EXISTS RawData (Date TEXT, Glucose REAL, BP INTEGER, Weight INTEGER)") 
    c.execute("INSERT INTO RawData (Date, Glucose, BP, Weight) VALUES (?, ?, ?, ?)", (time.strftime("%d/%m/%Y"), TodayReading[0], TodayReading[1] , TodayReading[2])) 
    conn.commit() 
    conn.close() 

def DataCheck(): 
    if ((float(TodayReading[0])>=4 and (float(TodayReading[0])<=6.9))): 
     colour = colourselection[count] 
     NAME OF ENTRY BOX HERE.configure(bg=colour) 

ありがとう:

これは、現時点では私のコードです。誰かが既にそれに答えているかもしれませんが、私はtkinterには新しいと言ったように、すでに見たことがあれば実装する方法は分かりません。

from tkinter import * 

class App: 
    def __init__(self, root): 
     self.root = root 
     self.var = StringVar() #creates StringVar to store contents of entry 
     self.var.trace(mode="w", callback=self.command) 
     #the above sets up a callback if the variable containing 
     #the value of the entry gets updated 
     self.entry = Entry(self.root, textvariable = self.var) 
     self.entry.pack() 
    def command(self, *args): 
     try: #trys to update the background to the entry contents 
      self.entry.config({"background": self.entry.get()}) 
     except: #if the above fails then it does the below 
      self.entry.config({"background": "White"}) 

root = Tk() 
App(root) 
root.mainloop() 

ので、上記entryウィジェットとそのウィジェットの内容が含まれているvariableを作成します。

+0

最後に、私が持っているもの項目によって、色が緑色、赤色、黄色に変わることを確認してください。 –

答えて

0

以下の私の例を参照してください。

tryが例外場合は白に背景を更新し、entry(IE、赤、緑、青)とexcept何らかのエラーの内容へentry背景色を更新するだろうvariableたちはcommand()を呼び出す更新されるたび上昇する。以下は


classを使用してentryの値をチェックするために別々のtestlistを使用せずにこれを行うための方法であって、Tkinterのインポートから *

root = Tk() 

global entry 
global colour 

def callback(*args): 
    for i in range(len(colour)): 
     if entry.get().lower() == test[i].lower(): 
      entry.configure({"background": colour[i]}) 
      break 
     else: 
      entry.configure({"background": "white"}) 


var = StringVar() 
entry = Entry(root, textvariable=var) 
test = ["Yes", "No", "Maybe"] 
colour = ["Red", "Green", "Blue"] 
var.trace(mode="w", callback=callback) 

entry.pack() 
root.mainloop() 
+0

申し訳ありませんが、これはばかげているかもしれませんが、私はどこでどのように私の仕事の中でこれを実装するのですか? –

+0

@BhavikNarang私は答えを更新しました –

+0

これは1つの入力ボックスですが、どのようにして各入力ボックスが個別にチェックされるようにするのですか?ループ内でフォームを作成したのがわかるので –