import random
import json
from tkinter import *
with open('worldcountries.json') as file_Obj:
data = json.load(file_Obj)
questions = [x for x in data[0].values()]
mcq_choices = [x for x in data[1].values()]
answers = [2,0,1,2,3,1,2,0,3,2]
candidate_ans = []
indexes = []
def showresult(score):
resultwin = Tk()
resultwin.title('Results')
if score < 20:
lblresult = Label(resultwin,text='Too bad!',font='Helvetica 20 bold',fg='red')
lblresult.pack(padx=20,pady=30)
if score >= 20 and score < 50:
lblresult = Label(resultwin,text='Good!',font='Helvetica 20 bold',fg='green')
lblresult.pack(padx=20,pady=30)
if score >= 50 and score < 80:
lblresult = Label(resultwin,text='Great!',font='Helvetica 20 bold',fg='blue')
lblresult.pack(padx=20,pady=30)
if score >= 80:
lblresult = Label(resultwin,text='Excellent!',font='Helvetica 20 bold',fg='yellow')
lblresult.pack(padx=20,pady=30)
def calculate():
global answers,candidate_ans,indexes
x = 0
score = 0
for x in indexes:
if candidate_ans[x] == answers[i]:
score += 10
x += 1
showresult(score)
def selected():
global mcqvar,candidate_ans,ques,m1,m2,m3,m4,ques
x = mcqvar.get()
candidate_ans.append(x)
ques = 1
if ques < len(questions):
lblquestion.config(text=questions[indexes[ques]])
m1['text'] = answers[indexes[ques]][0]
m2['text'] = answers[indexes[ques]][1]
m3['text'] = answers[indexes[ques]][2]
m4['text'] = answers[indexes[ques]][3]
ques += 1
else:
calculate()
def startquiz():
startwin = Tk()
startwin.title('Start!')
global indexes
while len(indexes) <= len(questions):
x = random.randint(0,9)
if x in indexes:
continue
else:
indexes.append(x)
global lblquestion
lblquestion = Label(startwin,text=questions[indexes[0]])
lblquestion.pack(padx=10,pady=10)
global mcqvar
mcqvar = IntVar()
global m1,m2,m3,m4
m1 = Radiobutton(startwin,text=mcq_choices[indexes[0]][0],value=0,variable=mcqvar,command=selected)
m1.pack(padx=10,pady=20)
m2 = Radiobutton(startwin,text=mcq_choices[indexes[0]][1],value=1,variable=mcqvar,command=selected)
m2.pack(padx=10,pady=20)
m3 = Radiobutton(startwin,text=mcq_choices[indexes[0]][2],value=2,variable=mcqvar,command=selected)
m3.pack(padx=10,pady=20)
m4 = Radiobutton(startwin,text=mcq_choices[indexes[0]][3],value=3,variable=mcqvar,command=selected)
m4.pack(padx=10,pady=20)
win = Tk()
win.title('Start quiz')
win.geometry('300x200')
lbquiz = Label(win,text='QUIZ',font='Helvetica 20 bold')
lbquiz.place(x=75,y=50)
btnstart = Button(win,text='Start',command=startquiz)
btnstart.place(x=100,y=150)
worldcountries.json
[
{
"1": "How many Keywords are there in C Programming language ?",
"2": "Which of the following functions takes A console Input in Python ?",
"3": "Which of the following is the capital of India ?",
"4": "Which of The Following is must to Execute a Python Code ?",
"5": "The Taj Mahal is located in ?",
"6": "The append Method adds value to the list at the ?",
"7": "Which of the following is not a costal city of india ?",
"8": "Which of The following is executed in browser(client side) ?",
"9": "Which of the following keyword is used to create a function in Python ?",
"10": "To Declare a Global variable in python we use the keyword ?"
},
{
"1": [
"23",
"32",
"33",
"43"
],
"2": [
"get()",
"input()",
"gets()",
"scan()"
],
"3": [
"Mumbai",
"Delhi",
"Chennai",
"Lucknow"
],
"4": [
"TURBO C",
"Py Interpreter",
"Notepad",
"IDE"
],
"5": [
"Patna",
"Delhi",
"Benaras",
"Agra"
],
"6": [
"custom location",
"end",
"center",
"beginning"
],
"7": [
"Bengluru",
"Kochin",
"Mumbai",
"vishakhapatnam"
],
"8": [
"perl",
"css",
"python",
"java"
],
"9": [
"function",
"void",
"fun",
"def"
],
"10": [
"all",
"var",
"let",
"global"
]
}
]
为了使程序能够成功运行,我已经尽力编写了这个程序。在我编写这个程序之前,我也仔细阅读了文档,并遵循了合适的方法。是不是我错过了什么让节目冻结了?请问当我按下按钮时,这个程序冻结的原因是什么?如果可能的话,有人能教我如何解决这个问题吗
您的代码有很多问题,包括但不限于:
mainloop
下面的脚本是一个完整的测试版本,解决了所有问题。
你没有使用任何关键的名字,所以我把它们去掉了
worldcountries.json
相关问题 更多 >
编程相关推荐