繁体   English   中英

需要帮助来识别我的代码中的错误。 Tkinter 消息框未显示所需 output

[英]Need help in identifying the mistake in my code. Tkinter messagebox not displaying desired output

def play():
    wind2 = tk.Toplevel()
    v = tk.IntVar()
    ques = ["Identify the least stable ion amongst the following.",
            "The set representing the correct order of first ionisation potential is",
            "The correct order of radii is"]
    o1 = ["", "Li⁺", "Be⁻", "B⁻", "C⁻"]
    o2 = ["", "K > Na > Li", "Be > Mg >Ca", "B >C > N", "Ge > Si >C"]
    o3 = ["", "N < Be < B", "F⁻ < O²⁻ < N³⁻", "Na < Li < K", "Fe³⁺ < Fe⁴⁺ < Fe²⁺"]
    choice = [o1, o2, o3]
    ans = [2, 2, 2]
    user_ans = []
    
    def selection():
        selected = v.get()
        for i in range(len(ques)):  
            if qsn["text"] == ques[i]:  
                break    
        if ans[i] == selected:
            print("Correct Answer")
            user_ans.append((i, selected))

    global score
    score = len(user_ans)*10
    def nxt():
        nxt.count += 1
        name.destroy()
        nmbox.destroy()
        nmbut.destroy()
        n = random.randint(0,2)
        qsn['text'] = ques[n]
        qsn.pack()
        r1['text'] = choice[n][1]
        r2['text'] = choice[n][2]
        r3['text'] = choice[n][3]
        r4['text'] = choice[n][4]
        r1.pack()
        r2.pack()
        r3.pack()
        r4.pack()
        nbut.pack()
        if nxt.count > 3:
            messagebox.showinfo("score", str(score))
    nxt.count = 0

    name = tk.Label(wind2, text = "Enter your name below:")
    nmbox = tk.Entry(wind2, bd = 4)
    nmbut = tk.Button(wind2, text = "Go", command = nxt)
    name.pack()
    nmbox.pack()
    nmbut.pack()
    
    qsn = tk.Label(wind2)
    r1 = tk.Radiobutton(wind2, variable = v, value = 1, command=selection)
    r2 = tk.Radiobutton(wind2, variable = v, value = 2, command=selection)
    r3 = tk.Radiobutton(wind2, variable = v, value = 3, command=selection)
    r4 = tk.Radiobutton(wind2, variable = v, value = 4, command=selection)
    nbut = tk.Button(wind2, text = "next", command = nxt)

选择正确选项后,(即当ans[i]=selected时) user_ans的长度当然不是0。那为什么messagebox每次返回score都是0? 我无法弄清楚我是否犯了任何错误。 基本上这是一个测验应用程序。 用户的分数是正确答案数的 10 倍。

首先,请下次提供可重现的代码示例。 您的脚本中有很多逻辑错误。 首先,当您运行play function 时,您会在开始时计算您的分数。此时您的列表user_ans仍然是空的。 如果您将分数计算移至nxt function,您最终会遇到不同的问题,例如,当有人一遍又一遍地点击正确答案时,您的分数可以 go 变为无穷大。 因此,您应该仅在用户单击next时评估用户选择。

这是一些重新排列。 这仍然不理想,但您可以从那里开始工作。

import tkinter as tk
import random
from tkinter import messagebox


def play():
    v = tk.IntVar()
    ques = ["Identify the least stable ion amongst the following.",
            "The set representing the correct order of first ionisation potential is",
            "The correct order of radii is"]
    o1 = ["", "Li⁺", "Be⁻", "B⁻", "C⁻"]
    o2 = ["", "K > Na > Li", "Be > Mg >Ca", "B >C > N", "Ge > Si >C"]
    o3 = ["", "N < Be < B", "F⁻ < O²⁻ < N³⁻", "Na < Li < K", "Fe³⁺ < Fe⁴⁺ < Fe²⁺"]
    choice = [o1, o2, o3]
    ans = [2, 2, 2]
    user_ans = []


    def selection():
        global selected
        selected = v.get()


    def nxt():
        nxt.count += 1
        name.destroy()
        nmbox.destroy()
        nmbut.destroy()
        n = random.randint(0,2)
        qsn['text'] = ques[n]
        qsn.pack()
        r1['text'] = choice[n][1]
        r2['text'] = choice[n][2]
        r3['text'] = choice[n][3]
        r4['text'] = choice[n][4]
        r1.pack()
        r2.pack()
        r3.pack()
        r4.pack()
        nbut.pack()

        for i in range(len(ques)):  
            if qsn["text"] == ques[i]:  
                break
        if ans[i] == selected:
            print("Correct Answer")
            user_ans.append(i)

        if nxt.count > 3:
            score = len(user_ans)*10
            messagebox.showinfo("score", str(score))

    nxt.count = 0

    name = tk.Label(root, text = "Enter your name below:")
    nmbox = tk.Entry(root, bd = 4)
    nmbut = tk.Button(root, text = "Go", command = nxt)
    name.pack()
    nmbox.pack()
    nmbut.pack()
    
    qsn = tk.Label(root)
    r1 = tk.Radiobutton(root, variable = v, value = 1, command=selection)
    r2 = tk.Radiobutton(root, variable = v, value = 2, command=selection)
    r3 = tk.Radiobutton(root, variable = v, value = 3, command=selection)
    r4 = tk.Radiobutton(root, variable = v, value = 4, command=selection)
    nbut = tk.Button(root, text = "next", command = nxt)

root = tk.Tk()
play()
root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM