· 3 years ago · May 08, 2022, 11:00 PM
1#Imports modules that are needed
2import tkinter as tk,webbrowser,sqlite3
3#Imports all within the module
4from tkinter import *
5
6#Creates introduction window
7intro = tk.Tk()
8#gives window title
9intro.title("Bullying and Well-being survey - Introduction")
10#window size dimensions
11intro.geometry("400x300")
12#configures the back ground colour of the window
13intro.configure(background = "lightgrey")
14
15#Creates a label for the window which displays a text with customisation
16label1 = tk.Label(intro, text ="Welcome to the bullying and wellbeing survey!\n - Created by Leander Esperanzate - \nPlease follow all instructions correctly",
17 fg="black",
18 bg = "lightgrey",
19 font=("Arial",10)).pack()#.pack() puts the widget in the frame
20
21
22
23
24
25
26#subroutine that destroys intro window and displays user window
27def next_user():
28 intro.destroy()
29
30#creates user window
31 username = tk.Tk()
32 username.title("Bullying and Well-being survey - Username")
33 username.configure(background = "lightgrey")
34
35#subroutine that creates and displays username
36 def user():
37 #gets values of input by .get() and stores in variables
38 name = f_name.get()
39 sname = s_name.get()
40 cnum = c_num.get()
41 #uses index and concantenation to create username
42 usernm = name[0] + sname[:3] + cnum[1:]
43 #label to display the username
44 tk.Label(username, text=f'Your username is: {usernm}', pady=20, fg= ("black"),bg = ("lightgrey"),font=("Arial",12)).grid()
45
46
47 #labels to prompt user to enter details
48 tk.Label(username, text="First Name:",bg = ("lightgrey"),font=("Arial",12)).grid(row=0)
49 tk.Label(username, text="Last Name:",bg = ("lightgrey"),font=("Arial",12)).grid(row=1)
50 tk.Label(username, text="Candidate number:",bg = ("lightgrey"),font=("Arial",12)).grid(row=2)
51 #entry box for the user to input data
52 f_name = tk.Entry(username)
53 s_name = tk.Entry(username)
54 c_num = tk.Entry(username)
55 #uses .grid() function and passes rows and columns for placement of the entry boxes
56 f_name.grid(row=0, column=1)
57 s_name.grid(row=1, column=1)
58 c_num.grid(row=2, column=1)
59 #button which calls user function when pressed
60 button2 = tk.Button(username,
61 text="Create username",
62 padx=10,
63 pady=5,
64 command=user
65 ).grid()
66 #subroutine that destroys user window and displays instruction window
67 def next_instr():
68 username.destroy()
69 instruct = tk.Tk()
70 instruct.title("Bullying and Well-being survey - Instructions")
71 instruct.configure(background = "lightgrey")
72 #displays instructions for the user
73 label2 = tk.Label(instruct,
74 text ="I will now begin to ask you a series of questions please answer as carefully and honestly as you can\nPlease answer from no , unsure or yes\n Press BEGIN to start questions",
75 fg="black",
76 bg = "lightgrey",
77 font=("Arial",10)).grid()
78
79 #subroutine that destroys instruction window and displays question window
80 def next_ques():
81 instruct.destroy()
82 ques = tk.Tk()
83 ques.title("Bullying and wellbeing survey - Questions")
84 #Creates first question and sets size and inserts it
85 """question 1"""
86 q1 = Text(ques, height = 1, width =100)
87 q1.insert(INSERT, "Question 1: Do you have any concerns about bullying in school")
88 q1.insert(END, "?")
89 q1.pack()
90 # function that takes the value of the chosen option and returns it
91 def o1():
92 output1 = var1.get()
93 return output1
94 #creates a variable for each score of each question and sets it to an integer variable
95 var1 = IntVar()
96 #creates radio button and assigns a variable, text , value and command
97 # anchor=w is used to position it horizontally and vertically around the left
98 Radiobutton(ques, text="No", variable=var1, value=1, command=o1).pack(anchor = W)
99 Radiobutton(ques, text="Unsure", variable=var1, value=2, command=o1).pack(anchor = W)
100 Radiobutton(ques, text="Yes", variable=var1, value=3, command=o1).pack(anchor = W)
101
102 #repeats creating and displaying buttons and questions for the rest of the question window
103
104 """question 2"""
105 q2 = Text(ques, height = 2, width =100)
106 q2.insert(INSERT, "Question 2: Do you have any concerns about someone else being bullied in school")
107 q2.insert(END, "?")
108 q2.pack()
109
110 def o2():
111 output2 = var2.get()
112 return output2
113
114
115 var2 = IntVar()
116 Radiobutton(ques, text="No", variable=var2, value=1, command=o2).pack(anchor = W)
117 Radiobutton(ques, text="Unsure", variable=var2, value=2, command=o2).pack(anchor = W)
118 Radiobutton(ques, text="Yes", variable=var2, value=3, command=o2).pack(anchor = W)
119
120 """question 3"""
121 q3 = Text(ques, height = 2, width =100)
122 q3.insert(INSERT, "Question 3: Have you ever been bullied")
123 q3.insert(END, "?")
124 q3.pack()
125
126 def o3():
127 output3 = var3.get()
128 return output3
129
130 var3 = IntVar()
131 Radiobutton(ques, text="No", variable=var3, value=1, command=o3).pack(anchor = W)
132 Radiobutton(ques, text="Unsure", variable=var3, value=2, command=o3).pack(anchor = W)
133 Radiobutton(ques, text="Yes", variable=var3, value=3, command=o3).pack(anchor = W)
134 """question 4"""
135 q4 = Text(ques, height = 2, width =100)
136 q4.insert(INSERT, "Question 4: Is school an unsafe environment for you")
137 q4.insert(END, "?")
138 q4.pack()
139
140 def o4():
141 output4 = var4.get()
142 return output4
143
144
145
146 var4 = IntVar()
147 Radiobutton(ques, text="No", variable=var4, value=1, command=o4).pack(anchor = W)
148 Radiobutton(ques, text="Unsure", variable=var4, value=2, command=o4).pack(anchor = W)
149 Radiobutton(ques, text="Yes", variable=var4, value=3, command=o4).pack(anchor = W)
150 """question 5"""
151 q5 = Text(ques, height = 2, width =100)
152 q5.insert(INSERT, "Question 5: Are you unhappy to go to school")
153 q5.insert(END, "?")
154 q5.pack()
155
156 def o5():
157
158 output5 = var5.get()
159 return output5
160
161 var5 = IntVar()
162 Radiobutton(ques, text="No", variable=var5, value=1, command=o5).pack(anchor = W)
163 Radiobutton(ques, text="Unsure", variable=var5, value=2, command=o5).pack(anchor = W)
164 Radiobutton(ques, text="Yes", variable=var5, value=3, command=o5).pack(anchor = W)
165
166
167
168 #subroutine that creates the final score by adding previous question scores
169 #destroys question window and displays results window
170 def next_results():
171 final = var1.get() + var2.get() + var3.get() + var4.get() + var5.get()
172 ques.destroy()
173
174 res = tk.Tk()
175 res.title("Bullying and wellbeing survey - Results")
176 tk.Label(res, text="Thank you for completing the survey!\nHere are your results:",
177 width =50, bg = "lightgrey",fg = "black",font=("Arial",20,"bold")).grid()
178 #subroutine that is called by a button and destroys the result window
179 def quits():
180 res.destroy()
181 #using selection to choose the label that will be displayed and other actions that are done
182 #if the final score is more than or equal to 13, most concerned result, adds details to database
183 if final >= 13:
184 tk.Label(res, text="According to your results, We may have concerns about your wellbeing at school\nyou will be reffered onto an external database where we will continue to provide support in the future\nFor now please take your time to read the information on this website.",
185 fg= ("black"),font=("Arial",12)).grid()
186
187
188#cannot use variables as they were destroyed in previous windows, would have to rearrange the whole program
189 #Create table
190
191
192## conn = sqlite3.connect('survey_names.db')
193## c = conn.cursor()
194## c.execute("""CREATE TABLE IF NOT EXISTS names(
195## f_name text,
196## s_name text,
197## username text)""")
198## conn.commit()
199##
200## c.execute("INSERT INTO names VALUES (:f_name, :s_name, :username)",
201## {
202## 'f_name': name,
203## 's_name': sname,
204## 'username': usernm
205## })
206##
207##
208##
209##
210##
211## conn.commit()
212## conn.close()
213
214
215
216
217
218
219 # redirects to web site by a button
220 new = 1
221 url = "https://www.caba.org.uk/mental-health.html"
222 #subroutine that opens web site with url adn webbrowser modules
223 def openweb():
224 webbrowser.open(url,new=new)
225 #button used to click to open the website
226 webBtn = Button(res, text = "CLICK TO VISIT WEBSITE",command=openweb).grid()
227 #if score is between 12 and 10, displays possible concern result
228 elif final <= 12 and final >= 10:
229 tk.Label(res, text="According to your results, we do not have any concerns at this moment about your mental wellbeing but we are more than happy to provide you support\nIf you would like do some extra reading please on mental wellbeing click the button below\nIf you would like to quit click QUIT ",
230 fg= ("black"),font=("Arial",12)).grid()
231 new = 1
232 url = "https://www.caba.org.uk/mental-health.html"
233
234 def openweb():
235 webbrowser.open(url,new=new)
236
237
238
239 webBtn = Button(res, text = "CLICK TO VISIT WEBSITE",command=openweb).grid()
240 #button that quits the result window and ends survey
241 quitbtn = Button(res, text = "QUIT!", bg = ("red"), fg = ("black"), command = quits).grid()
242 #final result, no concerns score less than 5
243 else:
244 tk.Label(res, text="According to your results, we have no concerns about your mental wellbeing at all as of this moment\nThank you for using and participating in this survey, have a good day!\nClick QUIT to end the survey",
245 fg= ("black"),font=("Arial",12)).grid()
246 #adds another quit button
247 quitbtn = Button(res, text = "QUIT!", bg = ("red"), fg = ("black"), command = quits).grid()
248
249
250
251
252
253
254
255 #button to click when completing questions and calls results window
256
257 btn = Button(ques, text='FINISH',command= next_results,
258 bg="lightgrey",fg="black",font=("Arial",16,"bold"))
259 #placing button in specific coordinates
260 btn.place(x=65, y=100)
261 btn.pack()
262
263
264
265 #button to call question window
266 button4 = tk.Button(instruct,
267 text="CONTINUE",
268 fg="black",
269 bg="white",
270 command = next_ques).grid()
271
272
273
274
275
276 #button to call instruction window
277
278 button3 = tk.Button(username,
279 text="CONTINUE",
280 fg="black",
281 bg="white",
282 command = next_instr).grid()
283
284
285#button to call username window
286
287button1 = tk.Button(intro,text="CONTINUE",
288 fg="black",
289 bg="white",
290 command = next_user).pack()
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316#infinite loop to run the program
317intro.mainloop()
318
319