[英]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.