編輯:我編輯了代碼,以便它可以運行並定義了所有變量。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?
請您建立一個[MCVE(https://stackoverflow.com/help/mcve) – Feodoran
您需要向我們展示questions'的'定義。 – BoarGules
添加問題的定義@BoarGules – Naomi