Home > OS >  how to transfer Entry information in tkinter to a list
how to transfer Entry information in tkinter to a list

Time:10-28

from tkinter import *
root = Tk()
root.title('sorter')
# funktion for new entry sorter
number = 0
entry_information_list = []
def new_entry():
    global number
    global entry_information_list
    number  = 1
    # number
    number_of_name = Label(root, text=number, width=20)
    number_of_name.grid(row=number, column=0)
    #entry feeld
    new_entry = Entry(root, borderwidth=3, width=100)
    new_entry.grid(row=number, column=1, columnspan=4)

    entry_information_list.append(new_entry.get())
    print(entry_information_list)

#buttons
number_for_list = Button(root, text='Sort by number', borderwidth=3, width=20)
button_for_1_name = Button(root, text='Sort by first name', borderwidth=3, width=20)
button_for_2_name = Button(root, text='Sort by second name', borderwidth=3, width=20)
button_for_3_name = Button(root, text='Sort by third name', borderwidth=3, width=20)
new_entry_button = Button(root, text='New entry', borderwidth=3, command=new_entry, width=20)

#position and description of buttons
number_for_list.grid(row=0, column=0)
button_for_1_name.grid(row=0, column=1)
button_for_2_name.grid(row=0, column=2)
button_for_3_name.grid(row=0, column=3)
new_entry_button.grid(row=0, column=4)


root.mainloop()

When I try to see what information is in the list from the consol it shows that it is filled with empty elements. am i missin a line or do i use a compleatly different structure?

CodePudding user response:

One of the ways to solve this is to bind the newly created entry to for example enter key (return key) and have a function that will get data from that entry and append it to the list:


from tkinter import Tk, Label, Button, Entry


root = Tk()
root.title('sorter')

# function for new entry sorter
number = 0
entry_information_list = []


def append_data(event=None):
    data = event.widget.get()
    entry_information_list.append(data)
    print(entry_information_list)


def new_entry():
    global number
    number  = 1
    # number
    number_of_name = Label(root, text=number, width=20)
    number_of_name.grid(row=number, column=0)

    # entry field
    entry = Entry(root, borderwidth=3, width=100)
    entry.grid(row=number, column=1, columnspan=4)
    entry.bind('<Return>', append_data)


# buttons
number_for_list = Button(root, text='Sort by number', borderwidth=3, width=20)
button_for_1_name = Button(root, text='Sort by first name', borderwidth=3, width=20)
button_for_2_name = Button(root, text='Sort by second name', borderwidth=3, width=20)
button_for_3_name = Button(root, text='Sort by third name', borderwidth=3, width=20)
new_entry_button = Button(root, text='New entry', borderwidth=3, command=new_entry, width=20)

# position and description of buttons
number_for_list.grid(row=0, column=0)
button_for_1_name.grid(row=0, column=1)
button_for_2_name.grid(row=0, column=2)
button_for_3_name.grid(row=0, column=3)
new_entry_button.grid(row=0, column=4)

root.mainloop()

A few downsides are that if you press the return key multiple times, the same value will get appended twice so maybe use a set or a dictionary or index to replace the previous value. Then also you have to press return key each time and be in the correct entry. But otherwise at least you get the idea: you need an event to happen that will get data from entry, otherwise as in your case it happens almost immediately without giving the user a chance to enter any data.

CodePudding user response:

The problem is this line:

entry_information_list.append(new_entry.get())

You are calling new_entry.get() about a millisecond after creating the entry widget, far before the user has seen the widget much less had time to type into it.

Instead, you should be appending the widget itself:

entry_information_list.append(new_entry)

Later, when you are ready to process the data, a simple list comprehension will get all the data from all of the widgets:

data = [widget.get() for widget in entry_information_list]
  • Related