【问题标题】:Changing entry box background colour in tkinter在 tkinter 中更改输入框背景颜色
【发布时间】:2017-09-03 18:48:18
【问题描述】:

所以我一直在研究这个程序,我发现很难找出问题所在。我对 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 的新手,所以如果我已经看过它,我还没有想出如何实现它。

【问题讨论】:

  • 最后,我要做的是确保颜色根据条目变为绿色、红色、黄色。

标签: python-3.x tkinter background-color tkinter-entry


【解决方案1】:

请看下面我的例子:

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、红色、绿色、蓝色)和except 的内容任何错误,如果引发异常,将背景更新为白色。


以下是不使用class 并使用单独的test list 来检查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()

【讨论】:

  • 抱歉,这可能很愚蠢,但我将如何在我的工作中实现这一点,比如在哪里以及如何实现?
  • 问题是这是一个输入框,但我如何制作它以便单独检查每个输入框,因为如您所见,我已经在一个循环中制作了我的表单
  • 您可以创建一个 stringvar 列表并将每个分配给条目小部件,然后跟踪任何 stringvar 何时以相同方式更新。
  • 这可能真的很令人沮丧,但你能不能为此写一些代码让我更好地理解它
猜你喜欢
  • 2016-09-10
  • 2021-03-07
  • 2017-10-16
  • 2021-01-12
  • 2023-04-10
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多