gui_test_2.py
1    from tkinter import *
2    from questions import questions, choices, answer
3    # app = Quiz(root) ကို လှမ်းခေါ်သည်။
4    # မေးခွန်း လုပ်တဲ့ function ခေါ်
5    # အဖြေ လုပ်တဲ့ function ခေါ်
6    # မေးခွန်း တဲ့ function ခေါ်
7    # button 2ခု
8    # create_question() မေးခွန်းအတွက် ပြင်ဆင်သည်
9    # create_ans_opt() အဖြေအတွက် ပြင်ဆင်သည်
10   # display_question() မေးခွန်းတွေကို ဖော်ပြသည်။
11   # check_q() မေးခွန်းမှန် မမှန် စစ သည်။
12   # print_resulရလဒ်ကို ဖော်ပြသည်။
13   # back_btn()
14   # next_btn()
15   
16   class Quiz:
17       def __init__(self, master):
18           self.opt_selected = IntVar() # Answer ကို select လုပ်ဘို့ variable
19           self.qn_no = 0 # Iniatilize question no start from zero
20           self.correct = 0 # အမှန်တွေကို ရေတွက်ဘို့
21   
22           self.ques = self.create_question(master) # မေးခွန်း နံပါတ်ထည်‌ပေး
23           self.opts = self.create_ans_opt(master, n=4) # အဖြေ option အရေအတွက်
24           self.display_question(self.qn_no) # မေးခွန်း နံပါတ်ထည်‌ပေး
25   
26           self.button2 = Button(master, text="Back", command=self.back_btn)
27           self.button2.pack(side=BOTTOM, pady=10)
28   
29           self.next_button = Button(master, text="Next", command=self.next_btn)
30           self.next_button.pack(side=BOTTOM)
31   
32       # create_question လုပ်ဘို့ qn_no ထည့်ပေး
33       #
34       def create_question(self, master):
35           b = Label(master, text="Geography Quiz")
36           labelfont = ('times', 20, 'bold')
37           b.config(font=labelfont)
38           b.pack(side=TOP, pady=3)
39   
40           w = Label(master, text= f"Question no and Question")
41           w.pack(side=TOP, pady=5)
42   
43           return w
44   
45   
46       def create_ans_opt(self, master, n):
47           b_val = 0
48           b = []
49           while b_val < n:
50               btn = Radiobutton(master, text="", variable=self.opt_selected, value=b_val+1)
51               b.append(btn)
52               btn.pack(side=TOP, padx=10, pady=2)
53               b_val = b_val + 1
54           return b
55   
56       def display_question(self, qn_no):
57           b_val = 0
58           self.opt_selected.set(0)
59           self.ques['text'] = f"Question No: {qn_no+1} \n {questions[qn_no]}"
60   
61           for op in choices[qn_no]:
62               self.opts[b_val]['text'] = op
63               b_val = b_val + 1
64   
65       def check_with_Answer(self, qn_no):
66           print(f"Question no: {qn_no}")
67           print(f"Candidate Answer: {self.opt_selected.get()} == Correct Answer: {answer[qn_no]}")
68           if self.opt_selected.get() == answer[qn_no]:
69               return True
70           return False
71   
72       # မေးခွန်း ကုန်ရင် print_results ကို ပြ , Quit ကို ထုတ်ပြ
73       def print_results(self):
74           print("Score: ", self.correct, "/", len(questions))
75           Button(root, text="Quit", command=root.destroy).pack(side=BOTTOM, padx=10, pady=10)
76   
77       def back_btn(self):
78           print("go back")
79   
80       # show individual result and increase qn_no
81       # next_btn ကို click လိုက်တာနဲ့ check_with_Answer() နဲ့ အဖြေကို စစ်
82       # နောက်မေးခွန်း အတွက် current question no + 1
83       # ၁ တိုးပြီး နောက်ဆုံး မေးခွန်း ဟုတ်မဟုတ် ( နောက်ထပ် မေးခွန်း ရှိ မရှိ စစ်)
84       def next_btn(self):
85           if self.check_with_Answer(self.qn_no):
86               print("Correct")
87               self.correct += 1
88               #print(f"Total correct{self.correct}/{self.qn_no+1}")
89           else:
90               print("Wrong")
91               #print(f"Total correct{self.correct}/{self.qn_no+1}")
92   
93           self.qn_no = self.qn_no + 1
94           if self.qn_no >= len(questions): # ညီသွားရင်ပြီး ပြီ, မေးခွန်း အသစ် မရှိတော့ဘူး
95               self.print_results()
96               self.next_button.pack_forget()
97               self.button2.pack_forget()
98           else:
99               self.display_question(self.qn_no) # မ ညီ သေးရင် display လုပ်စရာ မေးခွန်ရှိတယ်။ display လုပ်
100  
101  root = Tk()
102  root.title('Geography Quiz')
103  root.geometry("500x300")
104  app = Quiz(root)
105  root.mainloop()