# Using values from multiple entry box in tkinter

**URL:** <https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796>\
**Category:** Python Help\
**Tags:** help\
**Created:** [November 6, 2022, 5:58pm UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796 "2022-11-06T17:58:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hari-shreehari](https://avatars.discourse-cdn.com/v4/letter/h/bc8723/32.png) [@hari-shreehari](https://discuss.python.org/u/hari-shreehari)\
**Post date:** [November 6, 2022, 5:58pm UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796/1 "2022-11-06T17:58:22Z")

</div>

I’m trying to do a project with Tkinter, This program should be able to create new table in mysql. I want to create table-like entry boxes, I did it with a for loop, but I’m not sure how to obtain data that the user enters in that.

```auto
from tkinter import *
from tkinter import messagebox
import tkinter
import random
import mysql.connector as mc

ws = Tk()
ws.title('My SQL Table Creator')
ws.geometry('1920x1080')

con=mc.connect(host='localhost',user='root',passwd='hari@2468#')
cur=con.cursor()

def entry():
    global e1,e2,e3,e4
    p1=e1.get()
    p2=e2.get()
    p3=e3.get()
    p4=e4.get()
    
    cur.execute("Show databases")
    d=cur.fetchall()
    if (p1,) not in d:
        cur.execute(f'create database {p1}')
    cur.execute(f'use {p1}')
    cur.execute("Show tables")
    
    t=cur.fetchall()
    if (p2,) in t:
        messagebox.showinfo("Error",f"Table named {p2} already exists in provided database !! ")
    else:
        popup_win2=Toplevel()
        popup_win2.wm_title("Column headers ")
        cnamelst=[]
        cconstlst=[]
        choices=['Integer','Float','String','Date']
        for i in range(int(p3)):
            l=Label(popup_win2,text=f"Enter column {(i+1)} name : ")
            l.grid(row=20+(5*i), column=20)
            e=tkinter.Entry(popup_win2)
            e.grid(row=20+(5*i), column=50)
            opt=tkinter.StringVar(popup_win2)
            opt.set("Select data type constraints : ")
            c=tkinter.OptionMenu(popup_win2,opt ,*choices)
            c.grid(row=20+(5*i), column=80)
            cconstlst.append(str(opt.get()))
            cnamelst.append(str(e.get()))
        (Button(popup_win2,text="OK", command=(print(cnamelst,cconstlst)))).grid(row=(int(p3)*10),column=50)
        
        
    con.commit()

def register():
    global e1,e2,e3,e4
    (Label(ws,text="Enter the required details : ",font='arial 25 bold').pack)
    (Label(ws,text="Enter a new database name : ",font='arial 15 bold').place(x=10,y=30))
    e1=tkinter.Entry(ws)
    e1.place(x=350,y=35)
    (Label(ws,text="Enter a new table name : ",font='arial 15 bold').place(x=10,y=60))
    e2=tkinter.Entry(ws)
    e2.place(x=350,y=65)
    (Label(ws,text="Enter a number of Columns : ",font='arial 15 bold').place(x=10,y=90))
    e3=tkinter.Entry(ws)
    e3.place(x=350,y=95)
    (Label(ws,text="Enter a number of Rows : ",font='arial 15 bold').place(x=10,y=120))
    e4=tkinter.Entry(ws)
    e4.place(x=350,y=125)
    (Button(ws,text="Confirm", command=entry)).place(x=200,y=150)

register()
ws.mainloop()
con.close()

```

In this, I need the column name to be appended in cnamelst and constraints that user chooses to be appended in cconstlst.  
I’m not sure how to do so…  
I’ll be thankful if someone can teach me how to do that.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [November 6, 2022, 6:15pm UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796/2 "2022-11-06T18:15:03Z")

</div>

The Entry widget has a method `.get()` that will return its contents. All you need to do it save the reference to the widget when you create it.

```python
entries = []

for i in range(int(p3)):
    e = tkinter.Entry(popup_win2)
    entries.append(e)
    ...

```

Incidentally, it would be cleaner if you used only one way to refer to stuff from tkinter; I’d go with:

```python
import tkinter as tk

```

and then:

```python
ws = tk.Tk()

```

Also, better variable names. `e1`, `e2`, …? Not very clear. 🙂

---

<div class="post-metadata">

**Author:** ![hari-shreehari](https://avatars.discourse-cdn.com/v4/letter/h/bc8723/32.png) [@hari-shreehari](https://discuss.python.org/u/hari-shreehari)\
**Post date:** [November 7, 2022, 4:52pm UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796/3 "2022-11-07T16:52:21Z")

</div>

Hi Thanks a lot for trying to help me!  
And I’ll implement those suggestions very soon 😃

I tried to do from what I understood from your instruction, but I’m not sure if I did it right…

```auto
import tkinter as tk
ws=tk.Tk()
enteries=[]
for i in range(8):
    e=tk.Entry(ws)
    e.pack()
    enteries.append(e.get())

```

But after I give some values, I’m not sure how to append that value in the list enteries.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/8/89e452f8f7896912c8becac00c5762037c88d351.png)

Say In this, I want the details entered in those boxes to be updated into the list named enteries.  
But rather, the value with which it is being created ‘’ is being appended.

Pleas can you help me with this… 🙂

---

<div class="post-metadata">

**Author:** ![hari-shreehari](https://avatars.discourse-cdn.com/v4/letter/h/bc8723/32.png) [@hari-shreehari](https://discuss.python.org/u/hari-shreehari)\
**Post date:** [November 7, 2022, 5:06pm UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796/4 "2022-11-07T17:06:26Z")

</div>

Hi, thanks a lot !! I understood

---

<div class="post-metadata">

**Author:** ![hari-shreehari](https://avatars.discourse-cdn.com/v4/letter/h/bc8723/32.png) [@hari-shreehari](https://discuss.python.org/u/hari-shreehari)\
**Post date:** [November 7, 2022, 5:07pm UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796/5 "2022-11-07T17:07:34Z")

</div>

Sorry, I didn’t read it properly!! 🥺

```auto
import tkinter as tk
ws=tk.Tk()

def fetch():
    global l
    l=[]
    for i in enteries:
        l.append(i.get())
        
enteries=[]
for i in range(8):
    e=tk.Entry(ws)
    e.pack()
    enteries.append(e)
    
(tk.Button(ws,text='ok', command=(fetch))).pack()

```

I’ve made one out like this!!

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [November 7, 2022, 5:11pm UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796/6 "2022-11-07T17:11:51Z")

</div>

I didn’t put the `e.get()` (the contents of the widgets) into the list, I put `e` (the widgets themselves) into the list. To read the contents of the widgets, iterate over the list of widgets and use `e.get()` on each:

```python
import tkinter as tk

ws = tk.Tk()

# Make the Entry widgets and make a list of them.
entries = []

for i in range(8):
    e = tk.Entry(ws)
    e.pack()
    entries.append(e)

# Read the contents of the Entry widgets.
contents = []

for e in entries:
    contents.append(e.get())

```

---

<div class="post-metadata">

**Author:** ![hari-shreehari](https://avatars.discourse-cdn.com/v4/letter/h/bc8723/32.png) [@hari-shreehari](https://discuss.python.org/u/hari-shreehari)\
**Post date:** [November 11, 2022, 10:27am UTC](https://discuss.python.org/t/using-values-from-multiple-entry-box-in-tkinter/20796/7 "2022-11-11T10:27:45Z")

</div>

I’ve developed it a little more, But now in the last part where I should be getting the values in table, I need to add label containing the column name.  
I tried it, but ended unsuccessful messing the layout of table with each try…  
Please can you fix that part for me

```auto
from tkinter import *
from tkinter import messagebox
import tkinter
import random
import mysql.connector as mc

ws = Tk()
ws.title('My SQL Table Creator')
ws.geometry('1920x1080')
ws.config(bg='#82E0AA')

def pkey():
    global cur, liname,tbg,pop_win,opti
    pop_win=Toplevel()
    pop_win.geometry("640x420")
    opti=tkinter.StringVar(pop_win)
    opti.set("Select primary key : ")
    pk=tkinter.OptionMenu(pop_win,opti ,*liname)
    pk.grid(row=5,column=10)
    
    (Button(pop_win,text=" Set primary key ", command=build_table)).grid(row=10,column=10)

    
def create():
    global cur, tbg,le
    for i in le:
        v=()
        for j in i:
            v+=(eval(j.get()),)
        q=f"insert into {tbg} values {v}"
        cur.execute(q)
        con.commit()
    messagebox.showinfo("Success","Table created succesfully !! ")
    ws.destroy()
        
def build_table():
    global cur, rng,cng,le,liname,pop_win,opti
    pn=opti.get()
    q=f"alter table {tbg} add primary key ({pn})"
    cur.execute(q)
    pop_win.destroy()
    popup_win=Toplevel()
    popup_win.geometry('1920x1080')
    for k in range(len(liname)):
        t=liname[k]
        lb=Label(popup_win,text=f" {t} ")
        lb.grid(row=1,column=k*10)
    le=[]
    for i in range(int(rng)):
        lb.grid(row=i,column=1)
        se=[]
        for j in range(int(cng)):
            e=Entry(popup_win)
            e.grid(row=(i+1)*10,column=(j+1)*10)
            se.append(e)
        le.append(se)
    Button(popup_win, text=" Create table ", command=create).grid(row=(int(rng)+5)*10,column=(int(cng)+5)*10)
            
            
def fetch():
    global cur, licon, liname,q,tbg,popup_win2
    licon=[]
    liname=[]
    for i in cconstlst:
        licon.append(i.get())
    for j in cnamelst:
        liname.append(j.get())
    q='create table '
    if len(liname)==len(licon):
        for i in range(len(licon)):
            if licon[i] == 'String':
                licon[i]='Varchar(225)'
        qm=str(tbg)+' ('
        for a in range(len(liname)):
            qm+=liname[a]+' '+licon[a]+', '
        q=q+qm[:-2]+')'
        cur.execute(q)
    popup_win2.destroy()
    pkey()

        
def entry():
    global con,cur,dbe,tbe,cne,rne,cconstlst,cnamelst,dbg,tbg,cng,rng,popup_win2,une,pwe
    ung=une.get()
    pwg=pwe.get()
    dbg=dbe.get()
    tbg=tbe.get()
    cng=cne.get()
    rng=rne.get()

    con=mc.connect(host='localhost', username=ung, password=pwg)
    cur=con.cursor()
    cur.execute("Show databases")
    d=cur.fetchall()
    if (dbg,) not in d:
        cur.execute(f'create database {dbg}')
    cur.execute(f'use {dbg}')
    
    cur.execute("Show tables")
    t=cur.fetchall()
    if (tbg,) in t:
        messagebox.showinfo("Error",f"Table named {tbg} already exists in provided database !! ")
    else:
        popup_win2=Toplevel()
        popup_win2.wm_title("Column headers ")
        popup_win2.geometry("740x520")
        popup_win2.config(bg='#A569BD')
        cnamelst=[]
        cconstlst=[]
        choices=['Integer','Float','String','Date']
        for i in range(int(cng)):
            l=Label(popup_win2,text=f"Enter column {(i+1)} name : ")
            l.grid(row=20+(5*i), column=50)
            e=tkinter.Entry(popup_win2)
            e.grid(row=20+(5*i), column=150)
            opt=tkinter.StringVar(popup_win2)
            opt.set("Select data type constraints : ")
            c=tkinter.OptionMenu(popup_win2,opt ,*choices)
            c.grid(row=20+(5*i), column=250)
            cconstlst.append(opt)
            cnamelst.append(e)
        (Button(popup_win2,text="OK", command=(fetch))).grid(row=(int(cng)+5*10),column=200)       
    con.commit()

def register():
    global cur, dbe,tbe,cne,rne,une,pwe
    (Label(ws,text="Enter the required details : ",font='Magneto 30 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=30))
    (Label(ws,text="Enter your mysql user_name : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=120))
    une=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
    une.place(x=700,y=120)
    (Label(ws,text="Enter your mysql password : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=160))
    pwe=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
    pwe.place(x=700,y=160)
    (Label(ws,text="Enter a new database name : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=200))
    dbe=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
    dbe.place(x=700,y=200)
    (Label(ws,text="Enter a new table name : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=240))
    tbe=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
    tbe.place(x=700,y=240)
    (Label(ws,text="Enter a number of Columns : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=280))
    cne=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
    cne.place(x=700,y=280)
    (Label(ws,text="Enter a number of Rows : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=320))
    rne=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
    rne.place(x=700,y=320)

    
    (Button(ws,highlightbackground="#141414",highlightthickness=2,text="Confirm", command=entry)).place(x=600,y=380)

register()
con.commit()
ws.mainloop()
con.close()

```
