-3

我创建了一个数学游戏,当我加载第一个问题时它很好,如果我得到正确的答案,分数会增加 100,但是当它加载下一个问题时,它会直接将它加载到旧问题的顶部并且分数没有更长的增加。

我的计数器也无法结束游戏。

任何人都可以帮忙吗?

 def do_question(self):
            ##    def create_widgets(self):
                    #counter here if over 5 then die
                    counter = 0
                    counter += 1
                    #counter + 1
                    if counter > 5:
                        import ITRIED

                SQL = 'SELECT * FROM tblQuestion'
                cursor = Databaseconnector.SELECT(SQL)
                rows = cursor.fetchall()
                random_row = random.choice(rows)

                print random_row.QuestionID, random_row.Question, random_row.Hint, random_row.A1, random_row.A2, random_row.A3, random_row.A4, random_row.CorrectAnswer

                self.a1button = Tkinter.Button(self, background="blue",foreground="white", text = (random_row.A1), command = self.QUESTION1)
                self.a1button.grid(row = 9, column = 1, sticky = 'W')

问题检查器:

             def QUESTION1(self):
                score = int(self.label7['text'])
                if self.a1button['text'] == self.label6['text']:
                    tkMessageBox.showinfo("CORRECT", "WELL DONE")
                    score = +100
                    self.do_question()
                else:
                    tkMessageBox.showinfo("INCORRECT", "YOU GOT IT WRONG :/")
                    label7 = +100
                    self.do_question()
                self.label7.config(text=str(score))
4

1 回答 1

1

您的计数器永远不会超过 5,因为每次调用 do_question 时都会将其重置为零。另外,按照您的编码方式,计数器是一个局部变量。您需要做的第一件事是使 counter 成为实例变量(即:self.counter),这样它就不再是单个函数的本地变量。

score 变量也是如此——它是函数的局部变量,而不是实例变量。

至于为什么它“直接将它加载到旧的之上”,那是因为你告诉它这样做。在 do_question 中,您使用网格将按钮放置在第 9 行第 1 列。您永远不会删除第 9 行第 1 列可能已经存在的内容。在提出新问题之前,您应该在上一个问题上调用grid_removegrid_forget可见的。

于 2013-03-19T14:30:15.340 回答