Callback And N Entry Box Widgets Not Functioning Tkinter
Solution 1:
You have at least a couple of choices. For one, you can store store the entry widgets in a dict or list of lists, then pass the index into the callback. For example:
self.box = {}
foriinxrange(self.number_boxes):
row = row_number+add
column = self.column+i
key = "%d/%d" % (row,column)
...
self.box[key] = Entry(...)
self.box[key].bind("<Button-1>", lambda event, key=key: self.callback(event, key))
The important thing is to not just pick a method somebody gives you on the internet, but to understand the problem and the tools you have to solve it. If you take the time to learn how lambda works (or functools.partial) and have a basic understanding of fundamental data structures like lists and dicts, problems like this will cease to be stumbling blocks.
Solution 2:
For python newbies (like me) who are confused by this line
self.numbers = [StringVar() foriinxrange(self.number_boxes) ]
The keyword to google is 'list comprehension', this is a way to initialize a list in the format
[ expression for-clause ]
which is equivalent to the snippet
self.numbers = []
foriinxrange(self.number_boxes)
self.numbers.append(StringVar())
In other words it creates a list initialized as
[ StringVar, StringVar, StringVar, StringVar, ... ]
whose values are set later.
Post a Comment for "Callback And N Entry Box Widgets Not Functioning Tkinter"