Advertisement
acclivity

Python Tkinter Questionaire

Mar 14th, 2024
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | Software | 0 0
  1. import tkinter as tk
  2.  
  3. class QuestionnaireApp(tk.Tk):
  4.     def __init__(self):
  5.         super().__init__()
  6.         self.title("Questionnaire")
  7.         self.geometry("400x200")
  8.  
  9.         self.questions = [
  10.             (),
  11.             ("Question 1: Do you like Python?", 2, 3),
  12.             ("Question 2: Do you prefer cats over dogs?", 3, 4),
  13.             ("Question 3: Are you a morning person?", 5, 7),
  14.             ("Question 4: Do you enjoy reading books?", 6, 9),
  15.             ("Question 5: Is pizza your favorite food?", 8, 9),
  16.             ("Question 6: Do you like traveling?", 9, 10),
  17.             ("Question 7: Are you interested in learning new languages?", 5, 10),
  18.             ("Question 8: Do you enjoy outdoor activities?", 6, 10),
  19.             ("Question 9: Are you a fan of superhero movies?", 4, 10),
  20.             ("Question 10: Do you like solving puzzles?", 0, 0)
  21.         ]
  22.         self.current_question_index = 1
  23.  
  24.         self.data = self.questions[self.current_question_index]
  25.         self.quest = self.data[0]
  26.         self.yesnum = self.data[1]
  27.         self.nonum = self.data[2]
  28.         self.question_label = tk.Label(self, text=self.quest)       # text=self.questions[self.current_question_index])
  29.         self.question_label.pack()
  30.  
  31.         self.yes_button = tk.Button(self, text="YES", command=self.next_yes)
  32.         self.yes_button.pack(side=tk.LEFT, padx=10)
  33.  
  34.         self.no_button = tk.Button(self, text="NO", command=self.next_no)
  35.         self.no_button.pack(side=tk.RIGHT, padx=10)
  36.  
  37.     def next_yes(self):
  38.         self.current_question_index = self.yesnum
  39.         if self.current_question_index == 0:
  40.             self.question_label.config(text="Questionnaire completed.")
  41.         else:
  42.             self.data = self.questions[self.current_question_index]
  43.             self.quest = self.data[0]
  44.             self.yesnum = self.data[1]
  45.             self.nonum = self.data[2]
  46.             self.question_label.config(text=self.quest)
  47.  
  48.     def next_no(self):
  49.         self.current_question_index = self.nonum
  50.         if self.current_question_index == 0:
  51.             self.question_label.config(text="Questionnaire completed.")
  52.         else:
  53.             self.data = self.questions[self.current_question_index]
  54.             self.quest = self.data[0]
  55.             self.yesnum = self.data[1]
  56.             self.nonum = self.data[2]
  57.             self.question_label.config(text=self.quest)
  58.  
  59. if __name__ == "__main__":
  60.     app = QuestionnaireApp()
  61.     app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement