簡體   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