0

I have a project that allows the user to make a quiz on their own using some buttons and input

well i even want the user to be able to save their quiz in a file so they can load it in

i don't want something BIG!! a txt file will do..

i am using PySimpleGui and not Tkinter or anything..

i really don't know what i have made till now yet?(sorry i am not great with GUI)

i have a :

Main window
Editor window
And a form window

main window links the editor

editor links the form window

thanks for help in advance

if you need my code too then here

import pysimplegui as sg

layout = [
    [sg.Button("Make new Form")],
    [sg.Button("Open Form")]
]
window = sg.Window("Python Forms", layout)
def Form_Make():
    layout_for_make_form = [
    [sg.Button("Add multiple choice question")],
    [sg.Button("Save Form")]
    # some more items here..
    ]
    make_form_window = sg.Window("Make a Form..", layout_for_make_form)
    while True:
        events, values = make_form_window.read()
        if events == "Add multiple choice question":
            pass # this should add a new multi choice question(working on it!)
        elif events == "Save Form":
            # save a form.. i am stuck on this.. :|

while True:
    event,values = windows.read()
    if event == "Make new Form":
        Form_M()

i really don't know what it is doing yet i will have to make a new file and start from scratch :|

CodeMaster
  • 35
  • 1
  • 8

1 Answers1

1

this will work for you :

import PySimpleGUI as sg
import os.path

layout = [
    [sg.Button("Make new Form")],
    [sg.Button("Open Form")],
    [sg.Button("Exit")],
]
windows = sg.Window("Python Forms", layout)

questions = []
def Form_Make():
    layout_for_make_form = [
    [sg.Button("Add multiple choice question", key='add')],
    [sg.Text('file path',size=(10,1)),sg.FileBrowse(key='filepath')],
    [sg.Button("Save Form",key='save',visible=False)]
    # some more items here..
    ]
    make_form_window = sg.Window("Make a Form..", layout_for_make_form)
    while True:
        count = False
        events, values = make_form_window.read()
        if events == "add":
            layout_for_question = [
                [sg.Text('Must Enter all the filed for save question in file.')],
                [sg.Text('Enter Question : ',size=(10,1)),sg.Input(key='question')],
                [sg.Text('option1',size=(10,1)),sg.Input(key='option1')],
                [sg.Text('option2',size=(10,1)),sg.Input(key='option2')],
                [sg.Text('option3',size=(10,1)),sg.Input(key='option3')],
                [sg.Text('option4',size=(10,1)),sg.Input(key='option4')],
                [sg.Button('add')]
            ]
            make_question_window = sg.Window('question  ', layout_for_question)
            while True:
                events, values = make_question_window.read()
                if events == None:
                    break
                elif events == 'add' :
                    if values['question'] != '' and values['option1'] != '' and values['option2'] != '' and values['option3'] != '' and values['option4'] != '':
                        questions.append([values['question'],values['option1'],values['option2'],values['option3'],values['option4']])
                        print('value addded ')
                        count = True
            if count == True:
                make_form_window['save'].update(visible=True)
        elif events == "save":
            print(values['filepath'])
            file = values['filepath']
            if file != None:
                f=open(file,'w+')
                for x in questions:
                    for y in x:
                        f.write(y + '\n')
                f.close()
            print('save a form.. i am stuck on this.. :')
        elif events == None:
            break

while True:
    event,values = windows.read()
    if event == "Make new Form":
        Form_Make()
    elif event == 'Exit' or event == None :
        break
Bhargav Desai
  • 941
  • 1
  • 5
  • 17
  • May i know where the data is being stored? it doesn't seem to store to in the file i chose.. thanks in advance -CodeMaster – CodeMaster Jul 07 '20 at 06:19
  • data is store in selected file. after selecting file you have to save form. – Bhargav Desai Jul 07 '20 at 06:41
  • thats the problem i encountered when i ran the code.. trying to get it working.. it dosen't save/store the data in the file at all – CodeMaster Jul 07 '20 at 06:45
  • 1
    i will explain full path 1. make new form 2.add multiple choice question 3. must enter question and all the the option 4.click add button 5.optional(if you want multiple question change value and press add. repeat until your question is entered) 6. add txt file path using browse button 7. click save form . try this. – Bhargav Desai Jul 07 '20 at 07:09
  • File "C:\Users\USER\Desktop\Code\PYTHON\new.py", line 53, in Form_Make with open(file_name,'w+') as f: FileNotFoundError: [Errno 2] No such file or directory: '' – CodeMaster Jul 07 '20 at 07:57
  • Thanks alot Bhargav !! – CodeMaster Jul 07 '20 at 08:59
  • If it works, check it as the answer and press upvote plz. – Bhargav Desai Jul 07 '20 at 09:32
  • Sure i will do that – CodeMaster Jul 09 '20 at 05:04