Ok, I think I understand what you're trying to do here after reading this question and the one you asked earlier. You have a "database" (of sorts) of people who all have their own metadata and you want a way of displaying it in a tkinter window.
This is relatively simple and I think you're seriously, seriously over complicating things in your code.
The below example does what I think you need:
from tkinter import *
root = Tk()
class App:
def __init__(self, root):
self.root = root
self.data = [{"Name": "Alpha", "Var1": "3", "Var2": "1", "Var3": "4"},
{"Name": "Beta", "Var1": "1", "Var2": "5", "Var3": "9"},
{"Name": "Charlie", "Var1": "2", "Var2": "6", "Var3": "6"}
]
self.var = StringVar()
self.var.trace("w", self.callback)
self.entry = Entry(self.root, textvariable=self.var)
self.entry.grid(row=0, column=0, columnspan=2)
self.labels = [(Label(self.root, text="Name"),Label(self.root)),(Label(self.root, text="Var1"),Label(self.root)),(Label(self.root, text="Var2"),Label(self.root)),(Label(self.root, text="Var3"),Label(self.root))]
for i in range(len(self.labels)):
self.labels[i][0].grid(row = i+1, column=0)
self.labels[i][1].grid(row = i+1, column=1)
def callback(self, *args):
for i in self.data:
if i["Name"] == self.var.get():
for c in self.labels:
c[1].configure(text=i[c[0].cget("text")])
App(root)
root.mainloop()
So let's break this down.
First of all, I'm using a different data structure. In the above example we have a list
of dictionary
s. This is a pretty typical way of getting a "database like" structure in Python.
I'm then using a different method for actually calling the search, opting to use a binding on the variable
assigned to textvariable
within the Entry
widget. This means that every time the Entry
widget is written to a variable
is updated, whenever this variable
is updated we get a callback to the callback()
function, this section can be seen below:
self.var = StringVar() #we generate a tkinter variable class here
self.var.trace("w", self.callback) #we setup the callback here
self.entry = Entry(self.root, textvariable=self.var) #and we assign the variable to the Entry widget here
Next up I essentially create 6 Label
widgets in a 2 wide, 3 high grid. The Label
s on the left act as an identifier and the Label
s on the right are left blank for later. This is all pretty standard tkinter stuff which has been covered too many times on this site.
The last, and possibly hardest to understand section, is the actual callback, so let's look at it line-by-line:
for i in self.data:
This for
loop simply cycles through every dictionary, assigning it to i
.
if i["Name"] == self.var.get():
This if
statement says IF the value of the Entry
widget IS EQUAL TO the value of the Name
key in the dictionary we're currently looping through THEN True.
If the above check comes back as True
we then loop through self.labels
which contains the Label
widgets we generated earlier.
c[1].configure(text=i[c[0].cget("text")])
Lastly, we set the second value in each tuple
of the list
(The blank Label
widgets) to be the value of the dictionary we're looping through where the key matches the text of the identifier variable.
The above accomplishes what I think you want, you just need to amend your data to the format.