Advertisement
Guest User

Code questionnaire with interface

a guest
Sep 27th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.96 KB | Source Code | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, messagebox
  3.  
  4. # Questionnaire class that holds questions and logic for scoring
  5. class Questionnaire:
  6.     def __init__(self):
  7.         # Questions with reverse scoring flags
  8.         self.questions = [
  9.             {"question": "Is your workload unevenly distributed so it piles up?", "reverse_scoring": False, "type": "frequency"},
  10.             {"question": "How often do you not have time to complete all your work tasks?", "reverse_scoring": False, "type": "frequency"},
  11.             {"question": "Do you get behind with your work?", "reverse_scoring": False, "type": "frequency"},
  12.             {"question": "Do you have enough time for your work tasks?", "reverse_scoring": True, "type": "frequency"},  # Reverse scored
  13.             {"question": "How often do you consider looking for work elsewhere?", "reverse_scoring": True, "type": "frequency"},  # Reverse scored
  14.             {"question": "Does your work put you in emotionally disturbing situations?", "reverse_scoring": False, "type": "frequency"},
  15.             # Add more questions as needed...
  16.         ]
  17.         self.responses = []  # List to store user responses
  18.  
  19.     # Method to calculate score based on response
  20.     def calculate_score(self, question_index, response):
  21.         response_scale = {
  22.             "Always": 100,
  23.             "Often": 75,
  24.             "Sometimes": 50,
  25.             "Seldom": 25,
  26.             "Never/hardly ever": 0
  27.         }
  28.        
  29.         question = self.questions[question_index]
  30.         score = response_scale.get(response, 0)
  31.        
  32.         if question["reverse_scoring"]:
  33.             score = 100 - score  # Reverse the score if applicable
  34.        
  35.         # Store the response and score
  36.         self.responses.append((question["question"], response, score))
  37.         return score
  38.  
  39.     # Method to get a question by index
  40.     def get_question(self, index):
  41.         if index < len(self.questions):
  42.             return self.questions[index]
  43.         return None
  44.  
  45.     # Get all responses
  46.     def get_all_responses(self):
  47.         return self.responses
  48.  
  49. # GUI for the Questionnaire
  50. class GUIInterface:
  51.     def __init__(self, root, questionnaire):
  52.         self.root = root
  53.         self.questionnaire = questionnaire
  54.         self.current_question_index = 0
  55.  
  56.         # Setup the GUI elements
  57.         self.question_label = tk.Label(root, text="", wraplength=500)
  58.         self.question_label.pack(pady=20)
  59.  
  60.         self.selected_response = tk.StringVar()
  61.  
  62.         # Radio buttons for responses
  63.         self.radio_buttons = []
  64.         self.response_options = ["Always", "Often", "Sometimes", "Seldom", "Never/hardly ever"]
  65.         for option in self.response_options:
  66.             radio_btn = ttk.Radiobutton(root, text=option, variable=self.selected_response, value=option)
  67.             radio_btn.pack(anchor=tk.W)
  68.             self.radio_buttons.append(radio_btn)
  69.  
  70.         # Button to move to the next question
  71.         self.next_button = ttk.Button(root, text="Next", command=self.next_question)
  72.         self.next_button.pack(pady=10)
  73.  
  74.         # Start with the first question
  75.         self.display_question()
  76.  
  77.     # Display the current question
  78.     def display_question(self):
  79.         question_data = self.questionnaire.get_question(self.current_question_index)
  80.         if question_data:
  81.             self.question_label.config(text=question_data["question"])
  82.             self.selected_response.set(None)  # Clear the previous selection
  83.         else:
  84.             self.end_of_questions()
  85.  
  86.     # Handle the next button press
  87.     def next_question(self):
  88.         response = self.selected_response.get()
  89.         if response:
  90.             # Score the response
  91.             self.questionnaire.calculate_score(self.current_question_index, response)
  92.  
  93.             # Move to the next question
  94.             self.current_question_index += 1
  95.             self.display_question()
  96.         else:
  97.             messagebox.showwarning("Input Required", "Please select a response before proceeding.")
  98.  
  99.     # End of the questionnaire
  100.     def end_of_questions(self):
  101.         self.question_label.config(text="Thank you for completing the questionnaire!")
  102.         for radio_btn in self.radio_buttons:
  103.             radio_btn.pack_forget()  # Hide the radio buttons
  104.         self.next_button.pack_forget()  # Hide the next button
  105.  
  106.         # Show the results
  107.         results = self.questionnaire.get_all_responses()
  108.         result_text = "\n".join([f"Question: {q}\nResponse: {r}\nScore: {s}\n" for q, r, s in results])
  109.         messagebox.showinfo("Results", result_text)
  110.  
  111. # Main function to start the application
  112. if __name__ == "__main__":
  113.     # Create the root Tkinter window
  114.     root = tk.Tk()
  115.     root.title("COPSOQ II Questionnaire")
  116.     root.geometry("600x400")
  117.  
  118.     # Create the questionnaire object
  119.     questionnaire = Questionnaire()
  120.  
  121.     # Create the GUI interface
  122.     app = GUIInterface(root, questionnaire)
  123.  
  124.     # Start the Tkinter main loop
  125.     root.mainloop()
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement