0

Python tkinter lambda

I've got a list of Entrys like: name, address, ... BUTTON name, address, ... BUTTON name, address, ... BUTTON .... the button is to update a table in a database. So I created a function: def updateEntry(nr, tableName, entryList): print(nr, tableName) for E in entryList: print(E) The entrys and buttons are created in a loop: while x < len(Database-entrys): entry = Entry(second_frame) .... button = Button(second_frame, command= updateEntry(nr, tableName, entryList) But the button always showed the last number - no problem: I added lambda: button = Button(..., command = lambda nr = nr, tableName = tableName, entryList = entryList: updateEntry(nr, tableName, entryList), which worked, but only for the nr! So it shows the right number, but the entryList seems not to be affected by lambda. That's the part of my code, which I'm talking about x=0 zz=30 v=0 while(x<len(result)): y=0 Eintrag = result[x] while(y<len(Eintrag)): label = Entry(second_frame) #tk label.grid(row = zz, column= y) Ein =str(Eintrag[y]) label.insert(0, Ein) labels.append(label) labelReihe.append(label) y+=1 NR = str(Eintrag[0]) print("NR = " + NR) NRList.append(NR) #lambda anpassen btn_update = Button(second_frame, text="update", command = \ lambda NR=NR, tableName = tableName, labelReihe=labelReihe: updaten(NR, tableName, labelReihe)) btn_update.grid(row = zz, column = y+1) btnList.append(btn_update) btn_delete = Button(second_frame, text="delete", command = lambda NR = NR: deleten(NR)) btn_delete.grid(row = zz, column = y + 2) btnList.append(btn_delete) for L in labelReihe: print(L) del labelReihe[:] v+

27th Dec 2020, 10:51 AM
Fu Foy
Fu Foy - avatar
7 ответов
+ 1
Wow! Thank you, I'll take a close look at that! My output is: entry(ID1) entry(name1) entry(tel1) entry(address1) button entry(ID2) entry(name2) entry(tel2) entry(address2) button ... The information from the database is shown in entrys in order to update them later. So the button is supposed to run the update-def. In there it will be like "UPDATE tablename WHERE user_id = ID ...", then I want to update the table-values (id, name, address, tel ...) in my database with the values, displayed in the entry field. Therefore I thought to have a list of the entry-fields for each row connected with the button and the function it is calling and then update the database with the (new) values of those fields. I click button3, it calls the function and the list of entrys should contain the entryfields in the same row as the button, so that I can easily get the access to the new/updated information. So this list that is passed on button-click should not contain all the entrys, but only those, which are in the same row as the button.
27th Dec 2020, 3:13 PM
Fu Foy
Fu Foy - avatar
0
Not sure how you're set up, but if you are changing lables or text entries with a button, you can set a StrVar() as the textvariable in the entry or label. To change or delete: <text_variable_name>.set(<new value>)
27th Dec 2020, 11:57 AM
Slick
Slick - avatar
0
Thanks for your reply. I noticed, that my description is very ... confusing. Sorry for that. I tried to write an example: from tkinter import * import mysql.connector #Example of the result I get from an sql-query: sqlResult = {{Id, name, tel, address},{Id, name, tel, address},{Id, name, tel, address}} entryList = [] subEntryList = [] idList = [] tableName = "TableName" def mydef(tableName): x = 0 while x < len(sqlResult): subResult = sqlResult[x] zz = 0 y = 0 while y < len(subResult): entry = Entry(root) entry.grid(row = zz, column = y) subSubResult = str(subResult[y]) entry.insert(0, subSubResult) entryList.append(entry) subEntryList.append(entry) # supposed to append the subEntrys of each entry of y+=1 # the sqlResult subEntrylist = name1,tel1,address1 ID = str(subSubResult[0]) # the id from the sqlResult idList.append(ID) #Problem: the programm passes the right ID to the button and the function it # calls, but the subEntryList, that is passed is always the list of the last # entry (ID3, name3, tel3, address3), but it should be: # button1 takes ID1, tableName and subEntryList[ID1,name1,tel1, address1] #button2[ID2, name2 ... # but it is ID3, name3 ... for all buttons, while the first argument(ID) is # correct btn_update = Button(second_frame, text="update", command = \ lambda ID=ID, tableName = tableName,\ subEntryList=subEntryList : updaten(ID, tableName, subEntryList)) btn_update.grid(row = zz, column = y+1) btnList.append(btn_update) del subEntryList[:] #clear the list for the next loop def updaten(ID, tableName, subEntryList): #update the table in the database according to the entrys in the subEntryList
27th Dec 2020, 2:19 PM
Fu Foy
Fu Foy - avatar
0
Im having trouble visualizing how you're keeping the information, where you put it, and what you need to do with it. I just made a gui verion of this work order organizer that i made as a cli program. Look at 2 methods, form_check() and clear_form(). Basically, Im saying you can set up entry boxes and fill them with the data you recieve, then just call .get() on the text variable assigned and you have your value. You can easily clear them by using .set() [lines 271 & 284] https://code.sololearn.com/c4RCUx4wF4Po/?ref=app
27th Dec 2020, 2:44 PM
Slick
Slick - avatar
0
whenever I have to pick a certain value out of a list, i use a for loop with the enumerated list. You can check the index value and only display what you want based on that
27th Dec 2020, 3:20 PM
Slick
Slick - avatar
0
That's more complicated than my idea, but I think that will work. Thanks!
27th Dec 2020, 3:36 PM
Fu Foy
Fu Foy - avatar
0
@Slick I followed your advise and it worked! Thanks. I also found out, that I made a beginners mistake. I did not create my list in the function, but global! I thought, it was a problem with my button-command, but that solved the problem then.
28th Dec 2020, 2:06 PM
Fu Foy
Fu Foy - avatar