I try to code a to-do-list. When I click on the button self.button_check to change the color from a white entry to green it works until I create a second entry. The Problem is the new entry with the new check.button also look for the variable self.check_var which is used for the first. My Question is now: Is there a way to check the current bg color of an entry, to change if the entry is white or green instead of using the self.check_var variable?
Sry for obvious mistakes, I just started coding
import tkinter as tk
from tkinter.constants import ANCHOR, CENTER, X
class App():
def __init__(self):
self.window = tk.Tk()
self.window.title("To-Do-List")
self.window.geometry("700x700")
self.x_but, self.y_but = 0.05, 0.2
self.x_ent, self.y_ent = 0.05, 0.2
self.x_but2 = 0.3
self.check_var = True
self.start_frame()
self.grid1()
self.window.mainloop()
def start_frame(self):
self.label1 = tk.Label(text="To Do List", font=("", 30))
self.label1.place(relx=0.5, rely=0.05, anchor=CENTER)
def grid1(self):
self.button1 = tk.Button(text="Create", command= self.create_field)
self.button1.place(relx = self.x_but, rely= self.y_but)
def create_field(self):
self.y_but += 0.05
self.button1.place(relx= self.x_but, rely= self.y_but)
self.entry1 = tk.Entry()
self.entry1.place(relx= self.x_ent, rely= self.y_ent)
x = self.entry1
self.button_check = tk.Button(text="✅", height= 1,command=lambda x=self.entry1: self.check(x))
self.button_check.place(relx= self.x_but2, rely=self.y_ent)
self.y_ent += 0.05
def check(self,ent):
if self.check_var:
ent.configure(bg="Green")
self.check_var = False
else:
ent.configure(bg="White")
self.check_var = True
app = App()