0

In Tkinter how can I get and set the values gotten as result of iteration using the fuction "for x in range()". In the followin example the variable "sum" executes and operationg with the multiple results of "suma", I can print them, but how I direct them to specific variables if they are all named the same ("sum")?

min = 10
inc = 10
max = 40


for i in range(min,max,inc):
    sum = min + i
    print(sum)

Edition: I meant to say to collect the different values of "sum" and set them to either a label or entry, since I have to set the variable to show but that variables carries the same name, with different value for each iteration, though

Diego
  • 3
  • 3
  • You could store the values of i in an array, or perhaps a dictionary where the keys are i and the values are the sum? Is that what you are looking for? Could you edit your question to include a desired output? – plum 0 May 22 '20 at 18:44
  • I edited my post, I meant to say I want to be able to get the different values of "sum", show then in the window I´m working on, in a label of entry, but for either of those I have to set the variable to show, but in this case the variable has the same name for each different result – Diego May 22 '20 at 18:48

1 Answers1

0

You can store multiple values of sum by using an array or dictionary. I do not know anything about Tkinter but it appears you can use a function to set the text of a label (see answer to this question: How do I print an array in different lines in GUI window?)

It could look something like this:

min = 10
inc = 10
max = 40
sum = []


for i in range(min,max,inc):
    sum.append(min + i)
    print(sum[i])


def applytoLabel():
    n = len(sum)
    element = ''
    for i in range(n):
        element = element + sum[i]+'\n' 
    return element

label = tk.Label(canvas, text=applytoLabel(), font= "calibri 13", bg="white")

plum 0
  • 652
  • 9
  • 21
  • Thank you plum 0, I´m taking a look at the post you shared, I'm not currently familiar with arrays but will chek it out. Thanks again. – Diego May 22 '20 at 20:17