如何使用 Python 代码在一页上创建带有框的调查

问题描述 投票:0回答:1

我什至不知道这是否可能。我想为我的大学学习创建一个一页调查,该调查将呈现一个问题页面,如下所示在单页上如下所示(这是调查)...

...然后会将答案存储在程序中供我稍后查看。我正在大学外的训练营课程中学习 Python,但这是我在大学的统计课程。这不是一个编码课程,所以我不会通过寻求编程帮助来作弊。我只是想用 Python 启动这项在线调查,因为我知道如何充分使用 Python 来比使用任何其他编程语言更好地获取存储的数据,并且比纸质调查更快地获得结果。

我尝试过基本的输入输出版本,但更喜欢将所有问题放在一个页面上。如果可以的话请帮忙。如果这在 Python 中是不可能的,我对此表示怀疑,请告诉我,谢谢。

python user-interface frontend survey
1个回答
0
投票

我在工作,很无聊,所以我为你编程了:

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()
© www.soinside.com 2019 - 2024. All rights reserved.