Tkinter Python 3标签

2024-09-28 21:51:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用Tkinter做复习测验。程序将自动从数组中获取问题,然后使用标签输出问题。当按下submit按钮时,程序将检查答案,然后应该将标签更新到数组中的下一个问题,但是没有,没有错误消息。分数更新了,但是标签没有更新。你知道吗

def entryQuestion():

    entryOpen = open('./files/entry.csv','r')
    entryFile = csv.reader(entryOpen, delimiter = ',')

    global entryQuestionArray
    entryQuestionArray = []
    for topic, question, answer in entryFile:
        for i in range(0,1):
            entryQuestionArray.append(question)

    entryQuestionArray = random.sample(entryQuestionArray, len(entryQuestionArray))

    arrayLength = len(entryQuestionArray)

    for x in entryQuestionArray:
        global qOut
        qOut = x
        global entryQuestion
        entryQuestion = Label(entryQ, text = x)
        entryQuestion.grid(row = 1, column = 0, columnspan = 3)

        global answerEntry
        answerEntry = StringVar()
        global answerEntryBox
        answerEntryBox = Entry(entryQ, textvariable = answerEntry)
        answerEntryBox.grid(row = 2, column = 0, columnspan = 3)

        submitEntryAnswer = Button(entryQ, text = 'Submit Answer', command = entryQuestionCheck)
        submitEntryAnswer.grid(row = 3, column = 2)

def entryQuestionCheck():
    entryOpen = open('./files/entry.csv','r')
    entryFile = csv.reader(entryOpen, delimiter = ',')
    tempScore = score
    for topic, question, answer in entryFile:
        if qOut == question:
            if answerEntry.get() == answer:
                tempScore = tempScore + 1
            else:
                tempScore = tempScore +  0

    return

有人能帮忙吗?你知道吗


Tags: csvanswerinfor标签globalquestionanswerentry
1条回答
网友
1楼 · 发布于 2024-09-28 21:51:21

每个小部件只需要一个调用,在顶部或主类中,您只需要调用他一次:

class MainPage(#values):
    self.label1 = tk.Label(self, #values)

在函数中,如果你需要改变你的值,比如文本,你只需要把自我值传递给函数,然后改变他,像这样:

def changeValues(self):
    self.label["text"] = "the text that u want to put in"

或:

def changeValues(label):
    label["text"] = "the text that u want to put in"

在主类或作用域中,需要使用值调用函数,如果在作用域中(不在类中),则需要在值中传递小部件:

class MainPage(#values):
    self.label1 = tk.Label(self, #values)
    changeValues(self)

或:

label1 = tk.Label(self, #values)
changeValues(label1)

相关问题 更多 >