def check_win(): winner = None # Vertical lines if (board_squares[0][0].text == board_squares[0][1].text == board_squares[0][2].text) and board_squares[0][2].text in [ "X", "O" ]: winner = board_squares[0][0] elif (board_squares[1][0].text == board_squares[1][1].text == board_squares[1][2].text) and board_squares[1][2].text in ["X", "O"]: winner = board_squares[1][0] elif (board_squares[2][0].text == board_squares[2][1].text == board_squares[2][2].text) and board_squares[2][2].text in ["X", "O"]: winner = board_squares[2][0] # Horizontal lines elif (board_squares[0][0].text == board_squares[1][0].text == board_squares[2][0].text) and board_squares[2][0].text in ["X", "O"]: winner = board_squares[0][0] elif (board_squares[0][1].text == board_squares[1][1].text == board_squares[2][1].text) and board_squares[2][1].text in ["X", "O"]: winner = board_squares[0][1] elif (board_squares[0][2].text == board_squares[1][2].text == board_squares[2][2].text) and board_squares[2][2].text in ["X", "O"]: winner = board_squares[0][2] # Diagonals elif (board_squares[0][0].text == board_squares[1][1].text == board_squares[2][2].text) and board_squares[2][2].text in ["X", "O"]: winner = board_squares[0][0] elif (board_squares[2][0].text == board_squares[1][1].text == board_squares[0][2].text) and board_squares[0][2].text in ["X", "O"]: winner = board_squares[0][2] if winner is not None: message.value = winner.text + " wins!" elif moves_taken() == 9: message.value = "It's a draw" resetbutton = PushButton(board, text="reset", command=clear_board) resetbutton.hide() if winner is not None: resetbutton.show()
class Ultimate: def __init__(self, app): # score & highscore is being loaded in from a separate file start_up = open("startup_vars", "rb") get_dict_data = pickle.load(start_up) self.score = 0 self.highscore = get_dict_data.get("highscore") app = app # after each start of the quiz window, # reset the counter and randomize the question order global order_counter order_counter = 0 global questions_order random.shuffle(questions_order) #-----------------------------Ultimate Window--------------------------------# self.ultimate_window = Window(app, width=700, height=700, bg="#FFED7C") # Quiz contantainers from top to bottom; ultimate_container_1 = Image container, ultimate_container_2 = Question container, ultimate_container_3 = filler container, ultimate_container_4 = upper answer button container, ultimate_container_5 = filler container, ultimate_container_6 = lower answer button container, ultimate_container_7 = bottom container with score and question ultimate_container_1 = Box( self.ultimate_window, width=700, height=250) ultimate_container_2 = Box( self.ultimate_window, width=700, height=100, border=2) ultimate_container_3 = Box( self.ultimate_window, width=700, height=25) ultimate_container_4 = Box( self.ultimate_window, width=700, height=100) ultimate_container_5 = Box( self.ultimate_window, width=700, height=25) ultimate_container_6 = Box( self.ultimate_window, width=700, height=100) ultimate_container_7 = Box( self.ultimate_window, width=700, height=100) # containers where the upper buttons are positioned ultimate_filler_box_1 = Box(ultimate_container_4, align="left", width=150, height=100) ultimate_filler_box_2 = Box(ultimate_container_4, align="right", width=150, height=100) ultimate_button_1 = Box(ultimate_container_4, align="left", width=180, height=80) ultimate_button_2 = Box(ultimate_container_4, align="right", width=180, height=80) # containers where the lower buttons are positioned ultimate_filler_box_3 = Box(ultimate_container_6, align="left", width=150, height=100) ultimate_filler_box_4 = Box(ultimate_container_6, align="right", width=150, height=100) ultimate_button_3 = Box(ultimate_container_6, align="left", width=180, height=80) ultimate_button_4 = Box(ultimate_container_6, align="right", width=180, height=80) # bottom section with score and questions ultimate_bottom = Box(ultimate_container_7, align="bottom", width=700, height=50) ultimate_bottom_question = Box(ultimate_bottom, align="right", width=200, height=25, ) ultimate_bottom_score = Box(ultimate_bottom, align="left", width=100, height=25, ) ultimate_bottom_highscore = Box(ultimate_bottom, width=150, height=25, align="left", ) #-----------------------------Ultimate Widgets--------------------------------# # Question Image self.ultimate_image = Picture(ultimate_container_1, width=700, height=250) # Question Text self.ultimate_question = Text(ultimate_container_2, text="Question", width=100, height=100) self.ultimate_question.bg = "#FF8108" # Score, High Score & Question Num self.ultimate_question_number = Text(ultimate_bottom_question, text="Question Num: 1/10") self.ultimate_question_score = Text( ultimate_bottom_score, text="Score: 0") self.high_score_display = Text(ultimate_bottom_highscore, text="High Score: " + str(self.highscore), align="bottom") # Answer Buttons self.ultimate_answer_1 = PushButton(ultimate_button_1, align="left", width=100, height=70, command=self.check_a1) self.ultimate_answer_1.bg = "#54C03D" self.ultimate_answer_1.font = "sans-serif" self.ultimate_answer_2 = PushButton(ultimate_button_2, align="right", width=100, height=70, command=self.check_a2) self.ultimate_answer_2.bg = "#2D73A9" self.ultimate_answer_2.font = "sans-serif" self.ultimate_answer_3 = PushButton(ultimate_button_3, align="left", width=100, height=70, command=self.check_a3) self.ultimate_answer_3.bg = "#DB4692" self.ultimate_answer_3.font = "sans-serif" self.ultimate_answer_4 = PushButton(ultimate_button_4, align="right", width=100, height=70, command=self.check_a4) self.ultimate_answer_4.bg = "#FFFA13" self.ultimate_answer_4.font = "sans-serif" self.next_question() def next_question(self): global order_counter, menu_score_disp # ----------------------- check if end of the quiz ----------------------------- # if order_counter >= 40: # display the final score # maybe something that looks better than popup? # saves get_session_scores["score"] = self.score if self.score >= self.highscore: self.highscore = self.score self.high_score_display.value = "High Score: " + \ str(self.highscore) get_session_scores["highscore"] = self.highscore start_up = open("startup_vars", "wb") pickle.dump(get_session_scores, start_up) start_up.close() self.ultimate_window.info( "Congratulation", "Your score: " + str(self.score)+" /40") menu_score_disp = str(self.score) # in the later stages remember to pass info # if the user has passed the test # to unlock the ultimate test self.ultimate_window.destroy() try: # ------------------- update question and answers --------------------- # q = list_questions[questions_order[order_counter]].get_q_text() # Code below will set an image for the question if list_questions[questions_order[order_counter]].get_img_parameter() == "": self.ultimate_image.value = "imgs/img_blank.PNG" else: self.ultimate_image.value = list_questions[questions_order[order_counter] ].get_img_parameter() self.showAllAnswersButtons() # randomizing the answers answers = list_questions[questions_order[order_counter] ].get_randomize_answers() a1 = answers[0] a2 = answers[1] a3 = answers[2] a4 = answers[3] self.ultimate_question.value = q self.ultimate_answer_1.text = a1 self.ultimate_answer_2.text = a2 self.ultimate_answer_3.text = a3 self.ultimate_answer_4.text = a4 self.hideBlankButtons() # set the counter for the next question order_counter += 1 # ----------------------- update score and question number --------------------- # self.ultimate_question_score.value = "Score: " + str(self.score) self.ultimate_question_number.value = "Question Num: " + \ str(order_counter) + "/40" except IndexError: pass return menu_score_disp def check_a1(self): """this function is called after pressing answer button 1 it checks if the answer was correct and continue to the next question """ answer = self.ultimate_answer_1.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def check_a2(self): """this function is called after pressing answer button 2 it checks if the answer was correct and continue to the next question """ answer = self.ultimate_answer_2.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def check_a3(self): """this function is called after pressing answer button 3 it checks if the answer was correct and continue to the next question """ answer = self.ultimate_answer_3.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def check_a4(self): """this function is called after pressing answer button 4 it checks if the answer was correct and continue to the next question """ answer = self.ultimate_answer_4.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def showAllAnswersButtons(self): self.ultimate_answer_1.show() self.ultimate_answer_2.show() self.ultimate_answer_3.show() self.ultimate_answer_4.show() def hideBlankButtons(self): if self.ultimate_answer_1.text == "": self.ultimate_answer_1.hide() if self.ultimate_answer_2.text == "": self.ultimate_answer_2.hide() if self.ultimate_answer_3.text == "": self.ultimate_answer_3.hide() if self.ultimate_answer_4.text == "": self.ultimate_answer_4.hide()
class Mathematics: def __init__(self, app): self.score = 0 app = app # after each start of the quiz window, # reset the counter and randomize the question order global order_counter order_counter = 0 global questions_order random.shuffle(questions_order) #------------------------------Mathematics Window-----------------------------------# self.math_window = Window(app, width=850, height=700, bg="#FFED7C") # Quiz contantainers from top to bottom; mathematics_container_1 = Image container, mathematics_container_2 = Question container, mathematics_container_3 = filler container, mathematics_container_4 = upper answer button container, mathematics_container_5 = filler container, mathematics_container_6 = lower answer button container, mathematics_container_7 = bottom container with score and question mathematics_container_1 = Box(self.math_window, width=850, height=250) mathematics_container_2 = Box(self.math_window, width=850, height=100, border=2) mathematics_container_3 = Box(self.math_window, width=700, height=25) mathematics_container_4 = Box(self.math_window, width=700, height=100) mathematics_container_5 = Box(self.math_window, width=700, height=25) mathematics_container_6 = Box(self.math_window, width=700, height=100) mathematics_container_7 = Box(self.math_window, width=700, height=100) # containers where the upper buttons are positioned math_filler_box_1 = Box(mathematics_container_4, align="left", width=150, height=100) math_filler_box_2 = Box(mathematics_container_4, align="right", width=150, height=100) mathematics_button_1 = Box(mathematics_container_4, align="left", width=180, height=80) mathematics_button_2 = Box(mathematics_container_4, align="right", width=180, height=80) # containers where the lower buttons are positioned math_filler_box_3 = Box(mathematics_container_6, align="left", width=150, height=100) math_filler_box_4 = Box(mathematics_container_6, align="right", width=150, height=100) mathematics_button_3 = Box(mathematics_container_6, align="left", width=180, height=80) mathematics_button_4 = Box(mathematics_container_6, align="right", width=180, height=80) # bottom section with score and questions mathematics_bottom = Box(mathematics_container_7, align="bottom", width=700, height=50) mathematics_bottom_question = Box(mathematics_bottom, align="right", width=200, height=25) mathematics_bottom_score = Box(mathematics_bottom, align="left", width=100, height=25) #-----------------------------Mathematics Widgets------------------------------# # Question Image self.math_image = Picture(mathematics_container_1, width=850, height=250) # Question Text self.math_question = TextBox(mathematics_container_2, text="Question", width=100, height=100, multiline=True) self.math_question.bg = "#FF8108" self.math_question.font = "sans-serif" self.math_question.text_size = 12 # Score & Question Num self.math_question_number = Text(mathematics_bottom_question, text="Question Num: 1/10") self.math_question_score = Text(mathematics_bottom_score, text="Score: 0") # Answer Buttons self.math_answer_1 = PushButton(mathematics_button_1, align="left", width=100, height=70, command=self.check_a1) self.math_answer_1.bg = "#54C03D" self.math_answer_1.font = "sans-serif" self.math_answer_2 = PushButton(mathematics_button_2, align="right", width=100, height=70, command=self.check_a2) self.math_answer_2.bg = "#2D73A9" self.math_answer_2.font = "sans-serif" self.math_answer_3 = PushButton(mathematics_button_3, align="left", width=100, height=70, command=self.check_a3) self.math_answer_3.bg = "#DB4692" self.math_answer_3.font = "sans-serif" self.math_answer_4 = PushButton(mathematics_button_4, align="right", width=100, height=70, command=self.check_a4) self.math_answer_4.bg = "#FFFA13" self.math_answer_4.font = "sans-serif" self.next_question() def next_question(self): global order_counter # ----------------------- check if end of the quiz ----------------------------- # if order_counter >= 10: # display the final score # maybe something that looks better than popup? self.math_window.info("Congratulation", "Your score: " + str(self.score) + " /10") # in the later stages remember to pass info # if the user has passed the test # to unlock the ultimate test self.math_window.destroy() try: # ------------------- update question and answers --------------------- # q = list_questions[questions_order[order_counter]].get_q_text() # Code below will set an image for the question if list_questions[ questions_order[order_counter]].get_img_parameter() == "": self.math_image.value = "imgs/img_blank.PNG" else: self.math_image.value = list_questions[ questions_order[order_counter]].get_img_parameter() self.showAllAnswersButtons() # randomizing the answers answers = list_questions[ questions_order[order_counter]].get_randomize_answers() a1 = answers[0] a2 = answers[1] a3 = answers[2] a4 = answers[3] self.math_question.value = q self.math_answer_1.text = a1 self.math_answer_2.text = a2 self.math_answer_3.text = a3 self.math_answer_4.text = a4 self.hideBlankButtons() # set the counter for the next question order_counter += 1 # ----------------------- update score and question number --------------------- # self.math_question_score.value = "Score: " + str(self.score) self.math_question_number.value = "Question Num: " + \ str(order_counter) + "/10" except IndexError: pass def check_a1(self): """this function is called after pressing answer button 1 it checks if the answer was correct and continue to the next question """ answer = self.math_answer_1.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def check_a2(self): """this function is called after pressing answer button 2 it checks if the answer was correct and continue to the next question """ answer = self.math_answer_2.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def check_a3(self): """this function is called after pressing answer button 3 it checks if the answer was correct and continue to the next question """ answer = self.math_answer_3.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def check_a4(self): """this function is called after pressing answer button 4 it checks if the answer was correct and continue to the next question """ answer = self.math_answer_4.text print(answer) # the counter is already set for the next question # so we need to reduce it by one if list_questions[questions_order[order_counter - 1]].is_answer_correct(answer): self.score += 1 # after checking the answer we can move to the next question self.next_question() def showAllAnswersButtons(self): self.math_answer_1.show() self.math_answer_2.show() self.math_answer_3.show() self.math_answer_4.show() def hideBlankButtons(self): if self.math_answer_1.text == "": self.math_answer_1.hide() if self.math_answer_2.text == "": self.math_answer_2.hide() if self.math_answer_3.text == "": self.math_answer_3.hide() if self.math_answer_4.text == "": self.math_answer_4.hide()
bg=base_color, layout="auto") button_box = Box(app, width="fill", height=40, align="bottom") message_box = Box(app, border=4, height="fill", width="fill") message_box.bg = (255, 255, 255) message = TextBox(message_box, multiline=True, width="fill", height=350, visible=True) message.text_size = 16 chat = ChatHandlerWS() btn_connect = PushButton(button_box, text="Connect", align="left", command=connection_button, args=[chat], width=20, height=30, visible=True, padx=2, pady=2) btn_connect.text_size = 18 btn_connect.bg = "white" btn_connect.show() notify = Notifications() message_box.repeat(Notifications.CHECK_LOOP_MS, verify_chat, [chat]) app.display() closing(chat)