Advertisement
DrAungWinHtut

pyExamGuiDict.py

Jan 11th, 2024
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import tkinter as tk
  2. import tkinter.messagebox
  3. from tkinter import font
  4. import json
  5. from icecream import ic
  6.  
  7. global s_no
  8. global total_s_no
  9. global qlist
  10. global score
  11. global slide_index
  12.  
  13.  
  14. def fun_count_exam():    
  15.     with open('q.json','r') as qfile:
  16.         global total_s_no
  17.         global qlist
  18.         qlist = json.load(qfile)
  19.         total_s_no = len(qlist)
  20.        
  21.  
  22.  
  23. def fun_check():
  24.     global score
  25.     global s_no
  26.     global slide_index
  27.  
  28.     ans = txt_ans.get()
  29.     if ans == qlist[s_no]['correct_ans']:
  30.         tk.messagebox.showinfo('Correct!','Your answer is Correct!, Bravo!')
  31.         score += 1
  32.        
  33.     else:
  34.         tk.messagebox.showinfo('Fail!','Your answer is NOT Correct!, Sorry!')
  35.    
  36.     lbl_score.config(text=f"Total: {score}")    
  37.     s_no += 1
  38.     if s_no < total_s_no:
  39.         slide_index = 'Exam No. ' + str(s_no+1) + ' / ' + str(total_s_no)
  40.         lbl_slide_number.config(text=str(slide_index))
  41.         lbl_option1.config(text=qlist[s_no]['option1'])
  42.         lbl_option2.config(text=qlist[s_no]['option2'])
  43.         lbl_option3.config(text=qlist[s_no]['option3'])
  44.         txt_ans.delete(0, tk.END)
  45.     else:
  46.         tk.messagebox.showinfo('Congratulation! You answered all questions')
  47.         btn_check.config(state=tk.DISABLED)
  48.  
  49.  
  50.  
  51.  
  52. # Program Start Here
  53. score = 0
  54. s_no = 0
  55. total_s_no = 0
  56. fun_count_exam()
  57. window = tk.Tk()
  58. window.geometry('500x350')
  59.  
  60.  
  61.  
  62. slide_index = 'Exam No. ' + str(s_no+1) + ' / ' + str(total_s_no)
  63. lbl_slide_number = tk.Label(window, text=slide_index, font =('Courier',20, 'bold'))
  64. lbl_slide_number.pack()
  65.  
  66. str_question = qlist[s_no]['question']
  67. lbl_question = tk.Label(window, text=str_question, font =('Courier',20, 'bold'))
  68. lbl_question.pack()
  69.  
  70. str_option1 = qlist[s_no]['option1']
  71. lbl_option1 = tk.Label(window, text=str_option1, font =('Courier',20, 'bold'))
  72. lbl_option1.pack()
  73.  
  74. str_option2 = qlist[s_no]['option2']
  75. lbl_option2 = tk.Label(window, text=str_option2, font =('Courier',20, 'bold'))
  76. lbl_option2.pack()
  77.  
  78. str_option3 = qlist[s_no]['option3']
  79. lbl_option3 = tk.Label(window, text=str_option3, font =('Courier',20, 'bold'))
  80. lbl_option3.pack()
  81.  
  82.  
  83. txt_ans = tk.Entry(window, font =('Courier',20, 'bold'))
  84. txt_ans.pack()
  85.  
  86. btn_check = tk.Button(window,text='Check', font =('Courier',20, 'bold'),command=fun_check)
  87. btn_check.pack()
  88.  
  89. lbl_score = tk.Label(window, text='Total: ' + str(score), font =('Courier',20, 'bold'))
  90. lbl_score.pack()
  91.  
  92. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement