python tkinter Entry widget Dynamic Name

2019-09-19 09:39发布

anyone can guide me of the following:

I want to dynamically create the Entry widget and assigned hierarchical name (i.e. e1, e2, e3...etc), so that in next step easily reference back to collect the information.

see my draft code below, its not working

bowList = pd.DataFrame(np.empty([12,2]),columns=[['F','S']])

main2 = tki.Tk()
main2.minsize(800,500)

mb1 = tki.Menu(main2)

fm = tki.Menubutton(mb1)

l1 = tki.Label(main2, text="Fore Name")
l1.place(x=50,y=5,width=70)

l2 = tki.Label(main2, text="Surname")
l2.place(x=300,y=5,width=70)

h=1
for i in np.arange(1,12):
    h = i * 30 
    tki.Label(main2, text=i).place(x=10,y=h,width=15)
    e1 = tki.StringVar(main2)
    e1 ="e1%i" %(i)
    e2 = "e2%i" %(i)
    print(e1), print(e2)
    e1 = tki.Entry(main2, width=25, textvariable=e1)
    e1.place(x=50,y=h)
    e1.insert(i,i)

    e2 = tki.Entry(main2, width=25)
    e2.place(x=250,y=h)


def BowSave():
    for i in np.arange(1,12):
           #bowList.iloc[i,0] = (e11.get())
           #bowList.iloc[i,1] = (e21.get())
           #np.where(i == 10, print(bowList))
           #print(main2.children.values())
           #print(e11.get())


b1 = tki.Button(main2, text="Save", command=BowSave, height=2, width=5)
b1.place(x=500,y=5)

print(main2.winfo_name)
main2.mainloop()

Thanks, KP

1条回答
我只想做你的唯一
2楼-- · 2019-09-19 10:36

This snippet might get you started.

....

entry_text = ['e1', 'e2', 'e3']

....

    # add text entries
    for x, text in enumerate(entry_text):
        e = ttk.Entry(mFrame, width=80, name=text)
        e.grid(column=1, row=x, sticky='WE', padx=5, pady=5)
        e.insert(0, text)

....

    e1Text = master.children['mFrame'].children['e1'].get()
    e2Text = master.children['mFrame'].children['e2'].get()
查看更多
登录 后发表回答