Tkinter Python 3 - 标签

时间:2015-10-30 09:22:49

标签: python tkinter

我正在使用Tkinter制作修订测验程序。程序将自动从数组中获取问题,然后使用Label输出问题。当按下提交按钮时,程序将检查答案,然后应该将标签更新为数组中的下一个问题但是没有,没有错误消息。分数已更新,但标签不会更新。

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

每个小工具只需要1个电话,在顶部或主要课程中,您只需要打电话给他一次:

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

并且在函数中,如果你需要更改你的值,例如文本,你只需要将self值传递给function,并改变他,就像这样:

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)