Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk, messagebox
- # Questionnaire class that holds questions and logic for scoring
- class Questionnaire:
- def __init__(self):
- # Questions with reverse scoring flags
- self.questions = [
- {"question": "Is your workload unevenly distributed so it piles up?", "reverse_scoring": False, "type": "frequency"},
- {"question": "How often do you not have time to complete all your work tasks?", "reverse_scoring": False, "type": "frequency"},
- {"question": "Do you get behind with your work?", "reverse_scoring": False, "type": "frequency"},
- {"question": "Do you have enough time for your work tasks?", "reverse_scoring": True, "type": "frequency"}, # Reverse scored
- {"question": "How often do you consider looking for work elsewhere?", "reverse_scoring": True, "type": "frequency"}, # Reverse scored
- {"question": "Does your work put you in emotionally disturbing situations?", "reverse_scoring": False, "type": "frequency"},
- # Add more questions as needed...
- ]
- self.responses = [] # List to store user responses
- # Method to calculate score based on response
- def calculate_score(self, question_index, response):
- response_scale = {
- "Always": 100,
- "Often": 75,
- "Sometimes": 50,
- "Seldom": 25,
- "Never/hardly ever": 0
- }
- question = self.questions[question_index]
- score = response_scale.get(response, 0)
- if question["reverse_scoring"]:
- score = 100 - score # Reverse the score if applicable
- # Store the response and score
- self.responses.append((question["question"], response, score))
- return score
- # Method to get a question by index
- def get_question(self, index):
- if index < len(self.questions):
- return self.questions[index]
- return None
- # Get all responses
- def get_all_responses(self):
- return self.responses
- # GUI for the Questionnaire
- class GUIInterface:
- def __init__(self, root, questionnaire):
- self.root = root
- self.questionnaire = questionnaire
- self.current_question_index = 0
- # Setup the GUI elements
- self.question_label = tk.Label(root, text="", wraplength=500)
- self.question_label.pack(pady=20)
- self.selected_response = tk.StringVar()
- # Radio buttons for responses
- self.radio_buttons = []
- self.response_options = ["Always", "Often", "Sometimes", "Seldom", "Never/hardly ever"]
- for option in self.response_options:
- radio_btn = ttk.Radiobutton(root, text=option, variable=self.selected_response, value=option)
- radio_btn.pack(anchor=tk.W)
- self.radio_buttons.append(radio_btn)
- # Button to move to the next question
- self.next_button = ttk.Button(root, text="Next", command=self.next_question)
- self.next_button.pack(pady=10)
- # Start with the first question
- self.display_question()
- # Display the current question
- def display_question(self):
- question_data = self.questionnaire.get_question(self.current_question_index)
- if question_data:
- self.question_label.config(text=question_data["question"])
- self.selected_response.set(None) # Clear the previous selection
- else:
- self.end_of_questions()
- # Handle the next button press
- def next_question(self):
- response = self.selected_response.get()
- if response:
- # Score the response
- self.questionnaire.calculate_score(self.current_question_index, response)
- # Move to the next question
- self.current_question_index += 1
- self.display_question()
- else:
- messagebox.showwarning("Input Required", "Please select a response before proceeding.")
- # End of the questionnaire
- def end_of_questions(self):
- self.question_label.config(text="Thank you for completing the questionnaire!")
- for radio_btn in self.radio_buttons:
- radio_btn.pack_forget() # Hide the radio buttons
- self.next_button.pack_forget() # Hide the next button
- # Show the results
- results = self.questionnaire.get_all_responses()
- result_text = "\n".join([f"Question: {q}\nResponse: {r}\nScore: {s}\n" for q, r, s in results])
- messagebox.showinfo("Results", result_text)
- # Main function to start the application
- if __name__ == "__main__":
- # Create the root Tkinter window
- root = tk.Tk()
- root.title("COPSOQ II Questionnaire")
- root.geometry("600x400")
- # Create the questionnaire object
- questionnaire = Questionnaire()
- # Create the GUI interface
- app = GUIInterface(root, questionnaire)
- # Start the Tkinter main loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement