简体   繁体   中英

How to change the name of each entry box in tkinter when iterating in a for loop?

I want to create 20 input boxes in tkinter each holding an element of information from a list, I cannot work out how to loop through each input box displaying a new piece of data from the list. The list contains a simple string as each element. I want the loop to change result1 to result(i) eg result1 then result2 and so on so it matches the name of the textbox and each textbox has the next element of the string.

from tkinter import *
from tkinter.messagebox import *

def show_result():
    input = str(search.get())
    
    df_titles = pd.read_csv('movies.csv')
    df_titles.drop('genres', axis=1, inplace=True)
    df_titles['result'] = df_titles.astype(str).sum(axis=1).str.contains(input,case=False)
    df_results = df_titles.loc[df_titles['result'] == True]
    df_results.reset_index(drop=True)
    list_1 = df_results['title'].tolist()
    
    i = 0
    for i in range(20):
        result1.delete("0","end")
        result1.insert(0,list_1[i])
    
    



main = Tk()
Label(main, text = "Search:").grid(row=0)
Label(main, text = "Results:").grid(row=1)


search = Entry(main)
result1 = Entry(main)
result2 = Entry(main)
result3 = Entry(main)
result4 = Entry(main)
result5 = Entry(main)
result6 = Entry(main)
result7 = Entry(main)
result8 = Entry(main)
result9 = Entry(main)
result10 = Entry(main)
result11 = Entry(main)
result12 = Entry(main)
result13 = Entry(main)
result14 = Entry(main)
result15 = Entry(main)
result16 = Entry(main)
result17 = Entry(main)
result18 = Entry(main)
result19 = Entry(main)
result20 = Entry(main)


search.grid(row=0, column=1)
result1.grid(row=1, column=1)
result2.grid(row=2, column=1)
result3.grid(row=3, column=1)
result4.grid(row=4, column=1)
result5.grid(row=5, column=1)
result6.grid(row=6, column=1)
result7.grid(row=7, column=1)
result8.grid(row=8, column=1)
result9.grid(row=9, column=1)
result10.grid(row=10, column=1)
result11.grid(row=11, column=1)
result12.grid(row=12, column=1)
result13.grid(row=13, column=1)
result14.grid(row=14, column=1)
result15.grid(row=15, column=1)
result16.grid(row=16, column=1)
result17.grid(row=17, column=1)
result18.grid(row=18, column=1)
result19.grid(row=19, column=1)
result20.grid(row=20, column=1)


Button(main, text='Quit', command=main.destroy).grid(row=21, column=0, sticky=W, pady=4)
Button(main, text='Show', command=show_result).grid(row=21, column=1, sticky=W, pady=4)

mainloop()

Is this what you are looking for?

main = Tk()

Label(main, text = "Search:").grid(row=0)
search = Entry(main)
search.grid(row=0, column=1)

for i in range(1,21):
    Label(main, text = "Result {}:".format(i)).grid(row=i)
    Entry(main).grid(row=i, column=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM