首页 > 解决方案 > 如何在python中制作可保存的ui

问题描述

我有一个项目,允许用户使用一些按钮和输入自己进行测验

好吧,我什至希望用户能够将他们的测验保存在一个文件中,以便他们可以加载它

我不想要大的东西!!一个txt文件就可以了..

我正在使用 PySimpleGui 而不是 Tkinter 或任何东西..

我真的不知道我到现在为止做了什么?(对不起,我对 GUI 不是很好)

我有一个 :

Main window
Editor window
And a form window

主窗口链接编辑器

编辑器链接表单窗口

提前感谢您的帮助

如果你也需要我的代码,那么在这里

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()

我真的不知道它在做什么,但我将不得不制作一个新文件并从头开始:|

标签: pythonuser-interfacepysimplegui

解决方案


这对你有用:

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

推荐阅读