for循环只占用最后一个条目值(python / tkinter)

时间:2017-07-28 07:57:22

标签: python tkinter

编辑:我编辑了代码,因此它可以运行并且所有变量都已定义。

我有这段代码:

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 a scale of 1-10, is it moral?"))
for 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 the scale below. Click each number to view a description and set the values for each given emotion.")
Label1.pack(anchor = 'w')

frame2 = Frame(frame1)
frame2.pack()
for 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 a value between -10 and 10 for each emotion. Enter \"0\" for no effect.",
                           wraplength=700, justify=LEFT)
    label2.pack(anchor='w')
    button = Button(frame3, text="Continue", command=lambda: reset())
    for 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):
        for 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()

假设1按钮,用户输入“1”。对于2按钮,用户输入“2”。对于3按钮,用户输入“3”。

我的预期结果是:

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

相反,我得到:

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

如何修复它以获得理想的结果?

1 个答案:

答案 0 :(得分:0)

所有Emotion类实例中的answer实例都是完全相同的对象。因此,如果您为一个answer实例更改它,则会针对所有答案进行更改。

您可以通过比较内存中的地址来检查这一点。相同地址表示相同的对象。 (那里会有一个不同的地址,但它与所有实例相同。)

>>> questions.list[0].answers[0].emotions
[<__main__.Emotion instance at 0x7fdad37c7290>]
>>> questions.list[0].answers[1].emotions
[<__main__.Emotion instance at 0x7fdad37c7290>]

您需要更改其定义,因此它将变为特定于实例:

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

现在每个实例的地址都不同:

>>> questions.list[0].answers[0].emotions
[<__main__.Emotion instance at 0x7f4e4ce1ba28>]
>>> questions.list[0].answers[1].emotions
[<__main__.Emotion instance at 0x7f4e4b3575f0>]
相关问题