Guest User

Untitled

a guest
Sep 7th, 2024
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import csv
  2. from pyforms.basewidget import BaseWidget
  3. from pyforms.controls import ControlText, ControlButton, ControlLabel
  4.  
  5.  
  6. class QuestionsFormApp(BaseWidget):
  7.  
  8.     def __init__(self):
  9.         super().__init__('Questions Form')
  10.  
  11.         self._questions = []  # This will store questions from the CSV
  12.         self._dynamic_controls = []  
  13.  
  14.        
  15.         csv_file_path = 'questions.csv'
  16.  
  17.         # Load questions from the CSV file
  18.         self.load_questions_from_csv(csv_file_path)
  19.  
  20.         # Dynamically create controls based on the number of questions
  21.         for idx, question in enumerate(self._questions):
  22.             # Create a label for each question
  23.             label = ControlLabel(question)
  24.             # Create a text field for the answer
  25.             answer_field = ControlText(f'Answer {idx + 1}')
  26.             # Append them to the list of dynamic controls (used for submitting answers)
  27.             self._dynamic_controls.append(answer_field)
  28.  
  29.             # Add the controls to the form
  30.             setattr(self, f'label_{idx}', label)
  31.             setattr(self, f'answer_{idx}', answer_field)
  32.  
  33.         # Add a submit button
  34.         self._submit_btn = ControlButton('Submit')
  35.         self._submit_btn.value = self.__on_submit_click
  36.  
  37.         # Interleave question labels and answer fields using zip
  38.         form_elements = [elem for pair in zip([f'label_{idx}' for idx in range(len(self._questions))],
  39.                                               [f'answer_{idx}' for idx in range(len(self._questions))])
  40.                          for elem in pair] + ['_submit_btn']
  41.  
  42.         # Assign the formset
  43.         self.formset = form_elements
  44.  
  45.     def load_questions_from_csv(self, csv_file):
  46.         """Load questions from a CSV file."""
  47.         try:
  48.             with open(csv_file, 'r') as file:
  49.                 reader = csv.DictReader(file)
  50.                 for row in reader:
  51.                     self._questions.append(row['question'])
  52.         except FileNotFoundError:
  53.             print(f"Error: File {csv_file} not found.")
  54.         except KeyError:
  55.             print("Error: CSV file does not contain 'question' column.")
  56.  
  57.     def __on_submit_click(self):
  58.         """Handle the Submit button click."""
  59.         answers = {}
  60.         for idx, control in enumerate(self._dynamic_controls):
  61.             answers[self._questions[idx]] = control.value
  62.  
  63.         print("Answers submitted:", answers)
  64.         self.show_message("Form Submitted", "Your answers have been submitted.")
  65.  
  66.  
  67. if __name__ == '__main__':
  68.     from pyforms import start_app
  69.    
  70.     start_app(QuestionsFormApp, geometry=(200, 200, 500, 400))
  71.  
Advertisement
Add Comment
Please, Sign In to add comment