acclivity

pyTkinterQuestionnaire-non-OOP

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