简体   繁体   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)

After selecting the correct option, (ie when ans[i]=selected) the length of user_ans is of course not 0. Then why does the messagebox return score as 0 everytime?选择正确选项后,(即当ans[i]=selected时) user_ans的长度当然不是0。那为什么messagebox每次返回score都是0? I am unable to figure out if I have made any mistake.我无法弄清楚我是否犯了任何错误。 Basically this is a quiz application.基本上这是一个测验应用程序。 Score of the user is 10 times the number of correct answers.用户的分数是正确答案数的 10 倍。

First, please provide a reproducible code sample next time.首先,请下次提供可重现的代码示例。 There are a lot of logic errors in your script.您的脚本中有很多逻辑错误。 First you calculate your score in the beginning when you run the play function. At that point your list user_ans is still empty.首先,当您运行play function 时,您会在开始时计算您的分数。此时您的列表user_ans仍然是空的。 If you move your score calculation into the nxt function, you end up with different issues, for example your score can go into infinity when someone keeps clicking the right answer over and over.如果您将分数计算移至nxt function,您最终会遇到不同的问题,例如,当有人一遍又一遍地点击正确答案时,您的分数可以 go 变为无穷大。 So you should evaluate the user selection only once the user clicks next .因此,您应该仅在用户单击next时评估用户选择。

Here is some rearrangement.这是一些重新排列。 Still this is not ideal, but you can work from there.这仍然不理想,但您可以从那里开始工作。

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.

相关问题 需要一些帮助来识别 HTML 标签,这将使我能够提取所有相关的标题、链接和 img URL。 我的代码当前显示 1 - Need some help identifying the HTML tag that will allow me to pull all the relevant headlines, links and img URL's. My code is currently displaying 1 我需要帮助完成我的代码。 注意我是 Python 的初学者 - I need help completing my code. Note I am a beginner in Python 需要帮助来识别代码块中的缩进错误 - Need help identifying the indentation error in a code block MessageBox 未显示正确的 output - MessageBox not displaying the correct output 无法获取此代码的 output。 任何帮助都会得到帮助 - unable to get the output of this code. Any help will be appriciated TwilioQuest - -帮助,我的代码。 Python class 叫西铁城 - TwilioQuest - -Help, with my code. Python class called Citizen 我的代码有什么问题。 它继续显示此错误消息 - what is the problem with my code. it keeps on displaying this error message 谁能帮我在我的 python GUI 代码中找到错误? - Can anyone help me to find mistake in my python GUI code? 我需要帮助使用 tkinter 显示全屏图像 - I need help displaying full screen images with tkinter "需要帮助确定正确的 XPath" - Need help identifying right XPath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM