简体   繁体   中英

How do I automatically make python tkinter Entry automatically?

I've been making Entries in python tkinter but I found it out very inefficient to make every individual Entries. How can I make Entries automatically and get the values of all the Entries individually?

entry_word1 = tkinter.Entry(second_frame, width=30)
        entry_word1.grid(column=1, row=0)
        entry_word2 = tkinter.Entry(second_frame, width=30)
        entry_word2.grid(column=1, row=1)
        entry_word3 = tkinter.Entry(second_frame, width=30)
        entry_word3.grid(column=1, row=2)
        entry_word4 = tkinter.Entry(second_frame, width=30)
        entry_word4.grid(column=1, row=3)
        entry_word5 = tkinter.Entry(second_frame, width=30)
        entry_word5.grid(column=1, row=4)
        entry_word6 = tkinter.Entry(second_frame, width=30)
        entry_word6.grid(column=1, row=5)
        entry_word7 = tkinter.Entry(second_frame, width=30)
        entry_word7.grid(column=1, row=6)
        entry_word8 = tkinter.Entry(second_frame, width=30)
        entry_word8.grid(column=1, row=7)
        entry_word9 = tkinter.Entry(second_frame, width=30)
        entry_word9.grid(column=1, row=8)
        entry_word10 = tkinter.Entry(second_frame, width=30)
        entry_word10.grid(column=1, row=9)
      

Here is an example of storing Entry objects without using list

and accessing them by binding each one to a Return Key

with a callback to function getdata

Any Entry widget can be found using getEntry using index value.

import tkinter

master = tkinter.Tk()

second_frame = tkinter.Frame(master)
second_frame.grid(row=0, column=0, sticky='nsew')

def getdata(ev):
    print(ev.widget.get())

def getEntry( a ):
    return list( second_frame.children.values() )[ a ]

for a in range(10):
    b = tkinter.Entry(second_frame, width=30)
    b.grid(row=a, column=0, sticky='nsew')
    # important to bind each one for access
    b.bind('<Return>', getdata)

master.resizable(False, False)
master.mainloop()

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