如何创建一个包含输入的一页调查问卷
1 个回答
1
我在工作时觉得无聊,所以我为你写了这个程序:
from tkinter import *
import json
window = Tk()
window.geometry('550x350')
label1=Label(text='UNIVERSITY STUDENT SURVEY')
label1.pack()
firstname_label=Label(window,text='First Name')
firstname_label.place(x=0,y=25)
name_input=Text(window,height=1,width=40)
name_input.place(x=70,y=25)
date_label=Label(window,text='Date')
date_label.place(x=400,y=25)
date_input=Text(window,height=1,width=10)
date_input.place(x=450,y=25)
sleep_label=Label(window,text='What hours do you normally sleep?')
sleep_label.place(x=0,y=24*2)
bedtime_label=Label(window,text='Bedtime')
bedtime_label.place(x=20,y=25*3)
choices = []
for i in range(7,12):
choices.append(f'{i}.00pm')
for i in range(5):
choices.append(f'{i}.00am')
bedtimevariable = StringVar(window)
bedtimevariable.set('10.00pm')
bedtime_menu = OptionMenu(window, bedtimevariable, *choices)
bedtime_menu.place(x=70,y=25*3-5)
wakeup_label=Label(window,text='Wake Up Time')
wakeup_label.place(x=170,y=25*3)
choices = []
for i in range(4,13):
choices.append(f'{i}.00am')
wakeupvariable = StringVar(window)
wakeupvariable.set('8.00am')
wakeup_menu = OptionMenu(window, wakeupvariable, *choices)
wakeup_menu.place(x=250,y=25*3-5)
exercise_label=Label(window,text='Do you exercise?')
exercise_label.place(x=0,y=25*4)
choices = ['YES','NO']
exercisevariable= StringVar(window)
exercisevariable.set('YES')
exercise_menu = OptionMenu(window, exercisevariable, *choices)
exercise_menu.place(x=100,y=25*4-2)
epriority_label=Label(window,text='What priority is exercise?')
epriority_label.place(x=0,y=25*5)
evar=IntVar()
epriority_rbtn1=Radiobutton(window, text="High", variable=evar, value=1)
epriority_rbtn2=Radiobutton(window, text="Medium", variable=evar, value=2)
epriority_rbtn3=Radiobutton(window, text="Low", variable=evar, value=3)
epriority_rbtn1.place(x=150,y=25*5)
epriority_rbtn2.place(x=250,y=25*5)
epriority_rbtn3.place(x=350,y=25*5)
smoke_label=Label(window,text='Do you smoke?')
smoke_label.place(x=0,y=25*6)
choices = ['YES','NO']
smokevariable= StringVar(window)
smokevariable.set('YES')
smoke_menu = OptionMenu(window, smokevariable, *choices)
smoke_menu.place(x=100,y=25*6-2)
smokeyear_Label=Label(window,text='If yes, around what year did you start smoking?')
smokeyear_Label.place(x=0,y=25*7)
smokeyear_input=Text(window,height=1,width=10)
smokeyear_input.place(x=260,y=25*7)
smokeday_Label=Label(window,text='If yes, how many a day?')
smokeday_Label.place(x=0,y=25*8)
svar=IntVar()
smokeday_rbtn1=Radiobutton(window, text="10", variable=svar, value=1)
smokeday_rbtn2=Radiobutton(window, text="11-20", variable=svar, value=2)
smokeday_rbtn3=Radiobutton(window, text="20-40", variable=svar, value=3)
smokeday_rbtn4=Radiobutton(window, text="40+", variable=svar, value=4)
smokeday_rbtn1.place(x=150,y=25*8)
smokeday_rbtn2.place(x=250,y=25*8)
smokeday_rbtn3.place(x=350,y=25*8)
smokeday_rbtn4.place(x=450,y=25*8)
spriority_label=Label(window,text='If yes, what priority is smoking?')
spriority_label.place(x=0,y=25*9)
spvar=IntVar()
spriority_rbtn1=Radiobutton(window, text="High", variable=spvar, value=1)
spriority_rbtn2=Radiobutton(window, text="Medium", variable=spvar, value=2)
spriority_rbtn3=Radiobutton(window, text="Low", variable=spvar, value=3)
spriority_rbtn1.place(x=170,y=25*9)
spriority_rbtn2.place(x=270,y=25*9)
spriority_rbtn3.place(x=370,y=25*9)
smokeplace_Label=Label(window,text='If yes, where do you normally smoke?')
smokeplace_Label.place(x=0,y=25*10)
smokeplace_input=Text(window,height=1,width=10)
smokeplace_input.place(x=210,y=25*10)
privacy_Label=Label(window,font=("", 7),text='Thank you for taking part in this survey. Your information will be kept private under university privacy rules.')
privacy_Label.place(x=50,y=25*11)
def submit():
data={
'firstname':name_input.get("1.0","end-1c"),
'date':date_input.get("1.0","end-1c"),
'bedtime':bedtimevariable.get(),
'wakeuptime':wakeupvariable.get(),
'exercise':exercisevariable.get(),
'exercisepriority':'',
'smoke':smokevariable.get(),
'smokeyear':smokeyear_input.get("1.0","end-1c"),
'smokeday':'',
'smokepriority':'',
'smokeplace':smokeplace_input.get("1.0","end-1c")
}
exec(f'data["exercisepriority"]=epriority_rbtn{evar.get()}["text"]')
exec(f'data["smokeday"]=smokeday_rbtn{evar.get()}["text"]')
exec(f'data["smokepriority"]=spriority_rbtn{evar.get()}["text"]')
print(data)
with open('data.data','w') as f:
f.write(json.dumps(data))
'''
If you want to load the data
with open('data.data','r') as f:
data=json.loads(f.read())
'''
submit_btn=Button(window,text='Submit',command=submit)
submit_btn.place(x=250,y=25*12)
window.mainloop()