有谁知道如何刷新我的Q?

时间:2013-03-19 11:16:51

标签: user-interface python-2.7 tkinter

我创建了一个数学游戏,当我加载第一个问题时,如果我得到正确的答案,分数增加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))

1 个答案:

答案 0 :(得分:1)

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

对于得分变量也是如此 - 它对函数是局部的而不是实例变量。

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