Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

class - Creating a score count Tkinter-Python

I am creating a maths game using python and Tkinter and want a score count in the corner but am struggling to do this.

Here is a very simple version of my code (removed the parts not used for the score count):

from tkinter import *

root = Tk()
score = 0


def change_score(score_addition_easy_number, score):
    score = score + 1
    score_addition_easy_number.config(text=score)
    return score_addition_easy_number
    return score1


class addition():
    def __init__(self, score):
        self.score = score

    def addition_questions_easy_q1(self):
        root = Tk()
        root.geometry("600x500")

        score_addition_easy_label = Label(root, text="Score count: ")
        score_addition_easy_label.place(x=25, y=100)

        score_addition_easy_number = Label(root, text=self.score)
        score_addition_easy_number.place(x=120, y=100)

        a_e_answer_2 = Button(root, text="8", font=("times", 30, "bold"), padx=190, command=lambda: [change_score(score_addition_easy_number, score), self.addition_questions_easy_q2()])
        a_e_answer_2.place(x=100, y=270)


    def addition_questions_easy_q2(self):
        root2 = Tk()
        root2.geometry("600x500")

        score_addition_easy_label = Label(root2, text="Score count: ")
        score_addition_easy_label.place(x=25, y=100)

        score_addition_easy_number = Label(root2, text=self.score)
        score_addition_easy_number.place(x=120, y=100)

        a_e_answer_2 = Button(root2, text="8", font=("times", 30, "bold"), padx=190, command=lambda: [change_score(score_addition_easy_number, score), self.addition_questions_easy_q3()])
        a_e_answer_2.place(x=100, y=270)

    def addition_questions_easy_q3(self):
        root3 = Tk()
        root.geometry("600x500")

        score_addition_easy_label = Label(root3, text="Score count: ")
        score_addition_easy_label.place(x=25, y=100)

        score_addition_easy_number = Label(root3, text=self.score)
        score_addition_easy_number.place(x=120, y=100)

        a_e_answer_2 = Button(root3, text="8", font=("times", 30, "bold"), padx=190)
        a_e_answer_2.place(x=100, y=270)




obj1 = addition(0)
obj1.addition_questions_easy_q1()

root.mainloop()

when you get the question correct the score count increases to 1 on the first page, however, when the second page opens the score count goes back to zero.

I haven't really used classes before so there could be a problem regarding that causing the score count not to work.

Any ideas? Thanks x


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...