1

I started to code with tkinter and class methods. I try to code a to-do-list where I can create multiple entries by pressing a button below the entry. And a button next to the entry should change the color of the entry if pressed. The problem now is, that when I create multiple entries, the buttons only change the latest entry. So my question is how do I specify the entry when created?

Sry for obvious mistakes, Im new to coding :p

import tkinter as tk
from tkinter.constants import ANCHOR, CENTER, X
from tkinter import messagebox

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)
        

        self.button_check = tk.Button(text="✅", height= 1,command=self.check)
        self.button_check.place(relx= self.x_but2, rely=self.y_ent)
       



        self.y_ent += 0.05
    
    def check(self):
        if self.check_var:
            self.entry1.configure(bg="Green")
            self.check_var = False

        else:
            self.entry1.configure(bg="White")
            self.check_var = True

    



app = App()
antal
  • 45
  • 4

1 Answers1

2

You are changing the bg of self.entry1 which keeps overwriting itself each time an entry is created, so when button is clicked, it is always the last entry. The easiest solution is to define a parameter for check and then pass the required entry as an argument to it.

So the method would be:

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

...and your button would be:

self.button_check = tk.Button(text="✅", height= 1,command=lambda x=self.entry1: self.check(x))

Also I hope you have a good reason to use place because using grid can make your life much easier, in this case.

For more explanation, read: Tkinter assign button command in loop with lambda

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • Thanks, but now the variable I defined for the green background doesn't work as planned. So when I check the first entry and check the second, I need 2 clicks to turn it green – antal Jan 01 '22 at 18:32
  • @antal Because each time you click a button the value of _global_ `self.check_var` gets changed. You have to think of a better approach, I think its best to ask a new question for that – Delrius Euphoria Jan 01 '22 at 18:42