Reputation: 3
i'm beginner and I would to like to know if there is some way to make a function that repeats itself changing valor the valor of some variables...
For example:
def example():
box1.delete(0,END) #Here I would like to change the variable "box1" to "box2" and "box3"
get1=box1.get() #Here I would like to change the variable "box1" to "box2" and "box3"
Well, I think that is all. I hope you can help me. Thank you!
Upvotes: 0
Views: 1118
Reputation: 32189
Why not create a list of the boxes instead of having separate variable names for each. It makes your code more manageable and makes it easier to perform tasks on the boxes all at once.
For example:
boxes = []
#In your code where you create a box, instead of assigning it to a new variable as:
box12 = Box(...)
#just do
boxes.append(Box(...))
then you can easily do operations on the list by iteration through it or through list comprehension.
box_vals = [a.get() for a in boxes]
Upvotes: 0
Reputation: 309831
Usually, you would make box
an argument to the function and call it repeatedly with all the boxes you want to operate on:
def example(box):
box.delete(0, END)
get = box.get()
...
for box in box1, box2, box3, box4:
example(box)
if example
actually returns something (e.g. the data that box.get
returned), you can use a list comprehension.
boxes = (box1, box2, box3, box4)
data = [example(box) for box in boxes]
Now data
is a list with one element from each element in boxes
.
Upvotes: 3