私はこのプログラムに取り組んできましたが、何が間違っているかを知ることは非常に難しいと感じています。私はかなり新しいので、これはかなりマイナーなことがあります。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
を作成します。
最後に、私が持っているもの項目によって、色が緑色、赤色、黄色に変わることを確認してください。 –