How to make so that added Entry, but not already available were deleted?

from tkinter import * root=Tk() root.title("Нумерация") def addFlat(): kom_kv1=Entry(root) kom_kv1.grid() def deleteFlat(): kom_kv1.grid_forget() kom_kv1=Entry(root) kom_kv1.grid() plus=Button(root,text='Добавить квартиру', command=addFlat) plus.grid() plus2=Button(root,text='Удалить квартиру', command=deleteFlat) plus2.grid() root.mainloop() 

    2 answers 2

    Writing to the kom_kv1 variable inside the function creates a new local variable and does not change the value of the global variable named kom_kv1 outside the function. If you need to write from a function to a global variable, you must explicitly indicate that you want to use a global variable:

     def addFlat(): global kom_kv1 kom_kv1=Entry(root) kom_kv1.grid() 

    If you need the ability to consistently delete added items, you need to remember them all, and not just one. And still it is necessary to agree, we delete since the oldest or from the newest ( FIFO or LIFO ).

    Suppose this is a FIFO (i.e. a queue — the first one added will be deleted first). We use the usual list as storage.

     from tkinter import * root=Tk() root.title("Нумерация") flats = [] counter = 1 def addFlat(): global counter kom_kv = Entry(root) kom_kv.grid() # Для наглядности в текстовое поле записываем его номер по порядку: kom_kv.insert(0, str(counter)) counter += 1 flats.append(kom_kv) def deleteFlat(): if flats: # Если список не пустой # Достаем из начала списка один элемент, и сразу удаляем из окна flats.pop(0).grid_forget() # Если нужно удалять начиная с последнего добавленного, то меняем на такую строку: # flats.pop().grid_forget() plus=Button(root,text='Добавить квартиру', command=addFlat) plus.grid() plus2=Button(root,text='Удалить квартиру', command=deleteFlat) plus2.grid() root.mainloop() 

    By the way, for deletion, it is better to use not grid_forget , but destroy ; otherwise, all "remote" controls will remain in memory, although they will not be displayed on the screen. grid_forget needed to temporarily hide an element so that it can be displayed again using the grid method.

    • Thank you very much начал the code began to delete , but for some reason not all of the newly added , but only the last added , and please tell me how to delete the ace added in sequence? - user327535 pm
    • Added an example with the ability to sequentially remove all previously added items. - insolor
    • Thank!!! very helpful =) well explained! - user327535
    • Can you please tell me, is there an option to write down the following code more elegantly? - user327535
    • In general, it is better to separate the question, the questions in the answers are not welcome. 1 - you can use one list for everything, store a set of dictionaries or your objects inside. 2 - for input it is enough one set of editable fields, for output you can use listbox or treeview. 3 - if lists are always of the same size, it is enough to check only one of them for emptiness. And note, I’m not having a comparison with zero (this condition will always be false, the list will never be zero), but simply if flats . - insolor
     from tkinter import * root=Tk() root.title("Нумерация") flats = [] flats1 = [] flats2 = [] flats3 = [] def addFlat(): kom_kv=Label(root,text='Введите номер квартиры с несколькими собственниками:') kom_kv.grid() kom_kv1=Entry(root) kom_kv1.grid() sobs=Label(root,text='Введите колличество собственников:') sobs.grid() sobs1=Entry(root) sobs1.grid() flats.append(kom_kv) flats1.append(kom_kv1) flats2.append(sobs) flats3.append(sobs1) def deleteFlat(): if flats!=0 and flats1!=0 and flats2!=0 and flats!=0: flats.pop().grid_forget() flats1.pop().grid_forget() flats2.pop().grid_forget() flats3.pop().grid_forget() plus=Button(root,text='Добавить квартиру', command=addFlat) plus.grid() plus2=Button(root,text='Удалить квартиру', command=deleteFlat) plus2.grid() root.mainloop()