I'm a python/Tkinter newbie, so in advance I'll ask you not to throw bananas/rotten tomatoes/feces in my direction.
I'm writing some code that will be used as a data logger which will be build to capture several data values. Currently the program reads a set of survey lines and data points from a mysql database and populates the existing values into Entry elements with a button next to each element. The concept is that when a user clicks a button next to the adjacent Entry element, the Entry value will be updated with the new data value.
The specific problem I am having resides in passing the Entry element name to a function to overwrite its contents:
for row in rows:
cnt=cnt+1
Label(frame, text="Station: "+str(row[2]), bg=backcolor).grid(row=cnt, column=0)
e1 = Entry(frame)
e1.config(width=5)
if row[5]!="":#ensure data value is not null, or insert empty space
e1.insert(0, str(row[5]))
else:
e1.insert(0, " ")
e1.grid(row=cnt,column=1)
e2 = Button(frame, text="Log Data "+str(row[0]), command=lambda: saveData(e1))
e2.grid(row=cnt, column=2)
...
def saveData(entryid):
print Entry.get(entryid)
The problem lies with the saveData(e1). It appears to only pass the last element generated by the For row in rows constructor. What am I missing here to tie each button to its specific Entry element?
BTW, I am using Python 2.7
Thanks in advance for your assistance.
Here's the solution that Curly Joe pointed me to:
def saveData(stationdata):
print Entry.get(stationdata)
def getData():
#DATA TO GET SQL RESULTS/PREPARE FRAMED CANVAS ELEMENT HERE FOR SCROLLBARS
for row in rows:
cnt=cnt+1
Label(frame, text="Station: "+str(row[2]), bg=backcolor).grid(row=cnt, column=0)
e1 = Entry(frame)#Data Entry
e1.config(width=5)
if row[5]!="":
e1.insert(0, str(row[5]))
else:
e1.insert(0, "NA")
e1.grid(row=cnt,column=1)
e2 = Button(frame, text="Log Data "+str(row[0]), command=partial(saveData, e1))
e2.grid(row=cnt, column=1)