for loop takes only last entry value (python/tkinter) -


edit: i've edited code runnable , variables defined.

i have bit of code:

from tkinter import *  root = tk()  class question_list:     list = []  class question:     answers = []     def __init__(self,content):         self.content = content  class emotion:     def __init__(self, name, value):         emotion.name = name         emotion.value = value  class answer:     emotions = [emotion("anger",0)]     def __init__(self, content):         self.content = content     questions = question_list() buttonlist = [] entries = [] clicked = 0  questions.list.append(question("on scale of 1-10, moral?")) num in range(10):     questions.list[0].answers.append(answer(num+1))  frame1 = frame(root) frame1.pack(anchor = 'w') label1=label(frame1, text = "you chose scale. examine scale below. click each number view description , set values each given emotion.") label1.pack(anchor = 'w')  frame2 = frame(frame1) frame2.pack() num in range(1,11):     btn = button(frame2, text=num, command = lambda num=num:scaleset(num))     btn.pack(side=left)     buttonlist.append(btn)   def scaleset(n):     global clicked     buttonlist[n - 1].config(state=disabled)     clicked += 1     frame3 = frame(frame2)     frame3.pack(anchor='w')     label2 = label(frame3, text="enter value between -10 , 10 each emotion. enter \"0\" no effect.",                            wraplength=700, justify=left)     label2.pack(anchor='w')     button = button(frame3, text="continue", command=lambda: reset())     number in range(len((questions.list[0].answers[0].emotions))):         frame = frame(frame3)         frame.pack(anchor='w')         label = label(frame, text=questions.list[0].answers[0].emotions[number].name)         label.pack(side=left)         entry = entry(frame)         entry.pack(side=right)         entries.append(entry)     frame4 = frame(frame3)     frame4.pack(anchor='w')     button2 = button(frame4, text="done", command=lambda:set_scaleanswers(frame4))     button2.pack(side=left)      def set_scaleanswers(frame):         num in range(len(questions.list[0].answers[0].emotions)):             questions.list[0].answers[clicked - 1].emotions[num].value = entries[num].get()             headerscale2 = label(frame, text="answer recorded.")             headerscale2.pack(side=right)             button.pack(anchor='w')             del entries[:]          button2.config(state=disabled)      def reset():         frame3.destroy()     root.mainloop() 

say 1 button, user inputs "1". 2 button, user inputs "2". 3 button, user inputs "3".

my expected results are:

questions.list[0].answers[0].emotions[0].value = 1 questions.list[0].answers[1].emotions[0].value = 2 questions.list[0].answers[2].emotions[0].value = 3

instead get:

questions.list[0].answers[0].emotions[0].value = 3 questions.list[0].answers[1].emotions[0].value = 3 questions.list[0].answers[2].emotions[0].value = 3

how fix desired result?

the emotion instance in answer class instances same object. if change 1 answer instance, changed answers.

you can check this, comparing addresses in memory. same address means same object. (you have different address there, same of instances.)

>>> questions.list[0].answers[0].emotions [<__main__.emotion instance @ 0x7fdad37c7290>] >>> questions.list[0].answers[1].emotions [<__main__.emotion instance @ 0x7fdad37c7290>] 

you need change definition, becomes instance specific:

class answer:     def __init__(self, content):         self.emotions = [emotion("anger",0)]         self.content = content 

now addresses different each instance:

>>> questions.list[0].answers[0].emotions [<__main__.emotion instance @ 0x7f4e4ce1ba28>] >>> questions.list[0].answers[1].emotions [<__main__.emotion instance @ 0x7f4e4b3575f0>] 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -