【问题标题】:Adding to a counter on each frame for a quiz in python tkinter?在 python tkinter 中添加到每帧的计数器以进行测验?
【发布时间】:2018-08-25 02:07:04
【问题描述】:

所以我已将我的代码简化为基础,以尝试使其正常工作。您按下“播放”的主页会将您带到“MathsQ1”页面,这是第一个问题。该页面有一个问题和四个按钮,其中一个是右键。我已经能够为正确和错误的按钮发出两个不同的命令,带您进入下一个问题。但我也希望正确的函数将 1 添加到称为“正确”的计数器。完成最后一个问题后,它将带您进入“MathsEnd”页面,然后将您的分数显示为“恭喜您获得(正确)/2”,如果有人告诉我我做错了什么,我将不胜感激。谢谢。

代码:

import tkinter as tk
from tkinter import ttk

#INITIALIZING
class MegaQuiz(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "THE MEGA POP QUIZ")
        self.geometry("930x2000")
        self.highlightbackground="#FF846B"

        self.correct = 0

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (MainPage, MathsQ1, MathsQ2, MathsEnd):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("MainPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class MainPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background="#FFC2B5")


        #Play BUTTON
        tk.Button(self, text="PLAY", width=10, 
                        command = lambda: controller.show_frame("MathsQ1")) .grid(column=1, row=11, columnspan=8, pady=20, sticky="nesw")

class MathsQ1(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background="#FFC2B5")

        tk.Label(self, text="Maths Quiz") .grid(column=2, row=0)

        tk.Label(self, text="Question 1") .grid(column=4, row=1, columnspan=5, sticky="nesw")

        tk.Button(self, text="wrong",
                  command=self.wrong) .grid(column=4, row=3, sticky="nesw")
        tk.Button(self, text="right",
                  command=self.right) .grid(column=4, row=5, sticky="nesw")
        tk.Button(self, text="wrong",
                  command=self.wrong) .grid(column=6, row=3, sticky="nesw")
        tk.Button(self, text="wrong",
                  command=self.wrong) .grid(column=6, row=5, sticky="nesw")

    def wrong(self):
        self.controller.show_frame("MathsQ2")

    def right(self):
        self.controller.show_frame("MathsQ2")

        self.controller.correct = self.controller.correct + 1

class MathsQ2(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background="#FFC2B5")

        tk.Label(self, text="Maths Quiz") .grid(column=2, row=0)

        tk.Label(self, text="Question 2") .grid(column=4, row=1, columnspan=5, sticky="nesw")

        tk.Button(self, text="wrong",
                  command=self.wrong) .grid(column=4, row=3, sticky="nesw")
        tk.Button(self, text="right",
                  command=self.right) .grid(column=4, row=5, sticky="nesw")
        tk.Button(self, text="wrong",
                  command=self.wrong) .grid(column=6, row=3, sticky="nesw")
        tk.Button(self, text="wrong",
                  command=self.wrong) .grid(column=6, row=5, sticky="nesw")

    def wrong(self):
        self.controller.show_frame("MathsEnd")

    def right(self):
        self.controller.show_frame("MathsEnd")

        self.controller.correct = self.controller.correct + 1

class MathsEnd(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background="#FFC2B5")

        tk.Label(self, text=("congrats you got " + str(self.controller.correct) + "/2")) .grid(column=1, row=1)

        tk.Button(self, text="Back To Home",
                            command = lambda: controller.show_frame("MainPage")) .grid(column=6, row=10, sticky="nesw", pady=10)


#RUNNING PROGRAM
app = MegaQuiz()

app.mainloop()

【问题讨论】:

  • 我对 Tkinter 不是很熟悉,但看起来当您初始化 frame 时,您将 MegaQuiz 实例作为参数传递,而您正在初始化的类只是创建它的副本。所以self.controller.correct并不是真正指原始Megaquiz实例的correct,而是MegaQuiz实例的本地副本的correct
  • @prithajnath: “看起来当您初始化框架时,您将 MegaQuiz 实例作为参数传递,而您正在初始化的类只是创建它的副本。 " - 不,没有复制。正在保存引用,但未复制对象。你的陈述的其余部分也是错误的。
  • @BryanOakley 啊,你是对的。就在我以为我知道 Python 的时候

标签: python function class tkinter counter


【解决方案1】:

您在初始化应用程序时初始化 MathsEnd,因此显示分数的标签使用程序开始时的 controller.correct 值,即 0。 您需要在 MathsEnd 类中创建一个更新标签的方法:

class MathsEnd(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background="#FFC2B5")

        # create label, pack seperately so it can be configured later
        self.score = tk.Label(self)
        self.score.grid(column=1, row=1)
        # initalise using method
        self.update_label()

        tk.Button(self, text="Back To Home",
                        command = lambda: controller.show_frame("MainPage")) 
        .grid(column=6, row=10, sticky="nesw", pady=10)

    # new method to update label contents
    def update_label(self):
        self.score.config(text='Congrats you got %s/2' % self.controller.correct)

(我还必须对您创建标签的方式进行一些更改)。

然后,您需要在分数发生变化时调用此方法来更新分数。

def right(self):
    self.controller.correct += 1
    # update score label
    self.controller.frames['MathsEnd'].update_label()

    self.controller.show_frame('MathsQ2')

【讨论】:

  • 感谢您的回复,我明白您的意思,但是将 MathsEnd 更改为您发送给我的内容仍然会给出“0”作为分数。我是否错误地增加了变量?
  • 增量没问题,你是在每次更新分数/显示 MathsEnd 窗口时调用该方法吗?
  • 是的,它可以工作,谢谢 AAAAAAAAAAH
  • 没问题。请考虑通过单击绿色勾号来接受此答案,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2021-05-05
相关资源
最近更新 更多