-1

I am trying to create the program that has an add button, When it is clicked, several different elements like entries and buttons should appear on the output window. Firstly I am not able to structure the display correctly and secondly I am not sure as to how to get the values entered by the user in entry widget of Tkinter. Here is the Code:

from tkinter import *
from tkinter import messagebox
lb = Tk()

def addentry():
    i = 3    // the 3 here should keep on incrementing so that the row goes on increasing as the user 
                keeps on adding different entries. This is for the display
    ent1 = Entry(lb, bd=5).grid(row =i ,column= 0)
    ent2 = Entry(lb, bd=5).grid(row = i, column=2)
    ent3 = Entry(lb, bd=5).grid(row = i, column=4)
    ent4 = Entry(lb, bd=5).grid(row = i , column=6)


addent = Button(lb, text = "Add Entry",command = addentry).grid(row = 0, column = 2)

1 Answers1

2

It's all about keepin references. References are used to identify objects.

import tkinter as tk

root = tk.Tk()
my_entries = []
entry_row = 1
def addentry():
    global entry_row
    ent = tk.Entry(root, bd=5)
    ent.grid(row =entry_row ,column= 0)

    my_entries.append(ent)
    entry_row = entry_row+1

def getter():
    for entry in my_entries:
        my_stuff = entry.get()
        print(my_stuff)
    

addent = tk.Button(root, text = "Add Entry",command = addentry)
addent.grid(row = 0, column = 0)
getent = tk.Button(root,text='get input', command= getter)
getent.grid(row=0, column=1)


root.mainloop()

In this exampel we keepin the references of the tk.Entry and the variable entry_row while we would like to work with later on. There are a bunch of solution for this. Here we had used a global variable and a list in the global namespace to access them.

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54