-1

When I return values to a set of text fields and expect the same set of entries to display in the corresponding buttons,am only able to return the last entered text value into all the buttons using Tkinter python 3. How to get the corresponding text values in the corresponding button fields? This is the respective code for it.

def inputt(self):
    x = int(self.number.get())
    entries_list=[]
    path_list=[]
    button_list=[]
    label_list=[]
    button_1_list=[]

    for i in range(0,x):
        l = Label(root,text="Device "+str(i+1)+":")
        l.grid(row=i+5,column=0)
        label_list.append(l)

        self.e = Entry(root)
        self.e.grid(row=i+5,column=1)
        entries_list.append(self.e)

        b1 = Button(root,text="Next",command=lambda:load(self,self.inputt))
        b1.grid(row=i+6,column=0)

def load(self, inputt):
    for i in range(0,x):
        b3=Button(root, text=self.e[i].get())
        b3.grid(row=i+100,column=0)
        button_list.append(b3)

For the given text entries,I want to return the values to Def(load) function's buttons.

stovfl
  • 14,998
  • 7
  • 24
  • 51
Anonymous
  • 11
  • 2
  • Am calling the inputt function's inside this load so that i can get the entries from that and show those values in the buttons of def(load) – Anonymous Dec 23 '19 at 09:36
  • Read [Python and Tkinter lambda function](https://stackoverflow.com/a/11005426/7414759) – stovfl Dec 23 '19 at 12:14

1 Answers1

0

The variables that declares widgets are needs to be declared separately, otherwise the last variable will overwrite the current widget and when it will called, it will show only last widget value.

This can be fixed using enumerating the list. The enumeration technique indexes objects as well as selects every object from a list at a same time.

Also it is required to declare every text variable for Entry widget is declared separately.For that every variable will be appended to list and then that list will enumerated when the variables are called.

Here's a Solution,

import tkinter
from tkinter import *

root = Tk()

entries_list = []
path_list = []
button_list = []
label_list = []
var_list = []
x = 5    #int variable that define how many times will loop run

for var in range(0,x): 
    #always define a variable type before adding it to Entry Box
    se = StringVar()
    var_list.append(se)

def inputt():
    # use enumerate for indexing the list
    for p,q in enumerate(var_list):
        l = Label(root,text="Device "+str(p+1)+":")
        l.grid(row=p,column=0)
        label_list.append(l)

        e = Entry(root, textvariable = var_list[p], width=25)
        e.grid(row=p, column=1)

    b1 = Button(root,text="Next",command=lambda:load())
    b1.grid(row=p+2,column=0)


def load():
    # Entered values will loaded to entries_list when next button is pressed
    for u,v in enumerate(var_list):
        entries_list.append(var_list[u].get())

    for j,k in enumerate(entries_list):
        b3=Button(root, text=k)
        b3.grid(row= j + x + 2,column=0,columnspan = 2,sticky = EW)
        button_list.append(b3)

inputt()
root.mainloop()
Divyesh Mehta
  • 191
  • 1
  • 9