更改tkinter中的输入框背景颜色 [英] Changing entry box background colour in tkinter

查看:22
本文介绍了更改tkinter中的输入框背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在做这个程序,我发现很难找出哪里出了问题。我对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小部件。

每次更新variable时,我们调用command(),它将tryentry背景颜色更新为entry(IE,Red,Green,Blue)和except任何错误的内容,并在引发异常时将背景更新为白色。


下面是一种不使用class并使用单独的testlist检查entry的值的方法: 从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()

这篇关于更改tkinter中的输入框背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆