2017-07-28 92 views
-1

編輯:我編輯了代碼,以便它可以運行並定義了所有變量。for循環只需要最後一個輸入值(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

如何解決它,所以我得到我想要的RESU LT?

+0

請您建立一個[MCVE(https://stackoverflow.com/help/mcve) – Feodoran

+0

您需要向我們展示questions'的'定義。 – BoarGules

+0

添加問題的定義@BoarGules – Naomi

回答

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>] 
相關問題