Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import random
- def show_frame(frame):
- frame.tkraise()
- def validateReg(username, password):
- userstr = username.get()
- passstr = password.get()
- print("Username entered :", userstr)
- print("Password entered :", passstr)
- text_file = open("passdoc.txt", "w")
- text_file.write(userstr+"\n")
- text_file.write(passstr+"\n")
- text_file.close()
- def close():
- root.destroy()
- def FindLogin(username, password):
- userstr = username.get()
- passstr = password.get()
- text_file = open("passdoc.txt", "r")
- login_info = text_file.read()
- if login_info == username and password:
- commad=close
- text_file.close()
- def show_question(question):
- global current_question
- current_question = question
- # configure the question label to display the question
- question_label.configure(text=question.question)
- # configure the radiobuttons to hold the answers
- answers = list(question.answers)
- random.shuffle(answers)
- for i in range(4):
- answer_radiobuttons[i].configure(text=answers[i])
- answer_var.set(-1) # causes buttons to show as unselected
- class Question():
- def __init__(self, question_string):
- subject, question,answer1,answer2,answer3,answer4 = question_string.split(":")
- self.subject = subject
- self.question = question
- self.correct_answer = answer1
- self.answers = (answer1, answer2, answer3, answer4)
- def check_answer():
- choice = answer_var.get()
- choice = int(choice)
- answer = answer_radiobuttons[choice].cget("text")
- if answer == current_question.correct_answer:
- result_label.configure(text="Correct")
- else:
- result_label.configure(text="Incorrect")
- window = tk.Tk()
- window.state('zoomed')
- window.rowconfigure(0,weight=1)
- window.columnconfigure(0, weight=1)
- register = tk.Frame(window)
- login = tk.Frame(window)
- subject = tk.Frame(window)
- english = tk.Frame(window)
- maths = tk.Frame(window)
- science = tk.Frame(window)
- computing = tk.Frame(window)
- history = tk.Frame(window)
- english_multiple = tk.Frame(window)
- english_fill = tk.Frame(window)
- english_quick = tk.Frame(window)
- english_spag = tk.Frame(window)
- difficulty = tk.Frame(window)
- diff_ks2 = tk.Frame(window)
- diff_ks3 = tk.Frame(window)
- diff_ks4 = tk.Frame(window)
- diff_adult = tk.Frame(window)
- game = tk.Frame(window)
- question_frame = tk.Frame(game)
- game2 = tk.Frame(window)
- for frame in (game,register,login,subject,english,maths,science,computing,history,difficulty,diff_ks2,diff_ks3,diff_ks4,diff_adult,english_multiple,english_fill,english_quick,english_spag,):
- frame.grid(row=0,column=0,sticky='nsew')
- #================Register frame
- register.configure(background='RoyalBlue4')
- register_title = tk.Label(register, text='Register', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
- register_title.pack(fill='x')
- lblInst = tk.Label(register, text='Please choose username and password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
- lbl = tk.Label(register, text="Username:", fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
- ent = tk.Entry(register)
- lbl2 = tk.Label(register, text='Password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
- ent2 = tk.Entry(register,show='*')
- lblInst.pack()
- lbl.pack()
- ent.pack()
- lbl2.pack()
- ent2.pack()
- register_btn = tk.Button(register, text='Next ->', font=('Chalkboard'), command=lambda:show_frame(login))
- register_btn.pack()
- #================Login frame
- login.configure(background='RoyalBlue4')
- login_title = tk.Label(login, text='Login', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
- login_title.pack(fill='x')
- lblInst = tk.Label(login, text='Please enter username and password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
- lbl = tk.Label(login, text="Username:", fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
- ent = tk.Entry(login)
- lbl2 = tk.Label(login, text='Password:', fg='snow', bg='RoyalBlue4', font=('Chalkboard'))
- ent2 = tk.Entry(login,show='*')
- lblInst.pack()
- lbl.pack()
- ent.pack()
- lbl2.pack()
- ent2.pack()
- login_btn = tk.Button(login, text='Next ->', font=('Chalkboard'), command=lambda:show_frame(subject))
- login_btn2 = tk.Button(login, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(register))
- login_btn.pack()
- login_btn2.pack()
- #================Subject frame
- subject.configure(background='RoyalBlue4')
- subject_title = tk.Label(subject, text='Subject selection', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
- subject_title.pack(fill='x')
- lblInst = tk.Label(subject, text='Please select a subject:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
- lblInst.pack()
- lbl.pack()
- fr = tk.Frame(subject, height=700, width=1200, bg='RoyalBlue4', borderwidth=30)
- fr.place(relx=.5, rely=.4, anchor="c")
- btnE = tk.Button(fr, text='English', command=lambda:show_frame(english), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnM = tk.Button(fr, text='Maths', command=lambda:show_frame(maths), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnS = tk.Button(fr, text='Science', command=lambda:show_frame(science), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnC = tk.Button(fr, text='Computing', command=lambda:show_frame(computing), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnH = tk.Button(fr, text='History', command=lambda:show_frame(history), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnE.pack()
- btnM.pack()
- btnS.pack()
- btnC.pack()
- btnH.pack()
- subject_button = tk.Button(subject, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(login))
- subject_button.pack()
- #================English game type frame
- english.configure(background='RoyalBlue4')
- english_title = tk.Label(english, text='English game type selection', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
- english_title.pack(fill='x')
- lblInst = tk.Label(english, text='Please select a game type:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
- lblInst.pack()
- lbl.pack()
- fr = tk.Frame(english, height=700, width=1200, bg='RoyalBlue4', borderwidth=30)
- fr.place(relx=.5, rely=.4, anchor="c")
- btnMC = tk.Button(fr, text='Multiple choice', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnMC.pack()
- btnFitB = tk.Button(fr, text='Fill in the blank', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnFitB.pack()
- btnQf= tk.Button(fr, text='Quick-fire questions', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnQf.pack()
- btnSPaG = tk.Button(fr, text='SPaG mode', command=lambda:show_frame(difficulty), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnSPaG.pack()
- english_button = tk.Button(english, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(subject))
- english_button.pack()
- #===============Difficulty selection
- difficulty.configure(background='RoyalBlue4')
- difficulty_title = tk.Label(difficulty, text='Difficulty selection', fg='snow', bg='RoyalBlue4', font=('Chalkboard', 25))
- difficulty_title.pack(fill='x')
- lblInst = tk.Label(difficulty, text='Please select a difficulty:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
- lblInst.pack()
- lbl.pack()
- fr = tk.Frame(difficulty, height=700, width=1200, bg='RoyalBlue4', borderwidth=30)
- fr.place(relx=.5, rely=.4, anchor="c")
- btn2 = tk.Button(fr, text='KS3', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btn2.pack()
- btn3 = tk.Button(fr, text='KS4', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btn3.pack()
- btn4 = tk.Button(fr, text='KS5', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btn4.pack()
- btnA = tk.Button(fr, text='Adult', command=lambda:show_frame(game), fg='gray4', bg='snow', height=2, width=30, font=('Chalkboard'))
- btnA.pack()
- difficulty_button = tk.Button(difficulty, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(english))
- difficulty_button.pack()
- #==============Game Frame
- game.configure(background='RoyalBlue4')
- game_title = tk.Label(game, text='Multiple Choice!', fg='snow', bg='RoyalBlue4',font=('Chalkboard', 25))
- game_title.pack(fill='x')
- question_frame.configure(background='White')
- questions = {"English": [], "Maths": [], "Science": []}
- lblInst = tk.Label(game, text='Question 1:', fg='snow', bg='RoyalBlue4', font=('Chalkboard SE', 25))
- lblInst.pack()
- question_label = tk.Label(question_frame, width=80, anchor="c")
- answer_var = tk.StringVar()
- answer_radiobuttons = (
- tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=0, command=check_answer),
- tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=1, command=check_answer),
- tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=2, command=check_answer),
- tk.Radiobutton(question_frame, anchor="w", variable=answer_var, value=3, command=check_answer),
- )
- result_label = tk.Label(game)
- question_label.pack(side="top", fill="x", anchor="c")
- result_label.pack(side="bottom", fill="x")
- for button in answer_radiobuttons:
- button.pack(side="top", fill="x", expand=True, anchor="n")
- f = open("questions.txt","r")
- for row in f.readlines():
- question = Question(row)
- questions[question.subject].append(question)
- show_question(question)
- f.close()
- question_frame.pack()
- #game_button = tk.Button(game, text='Submit', font=('Chalkboard'), command=lambda:check_answer)
- game_button2 = tk.Button(game, text='<- Back', font=('Chalkboard'), command=lambda:show_frame(difficulty))
- #game_button.pack()
- game_button2.pack()
- show_frame(register)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement