how to incorporate a .txt file into a window (pycharm)

Question:

import PySimpleGUI as sg

sg.theme("LightGray1")

layout1 = [ #MAIN PAGE
   [sg.Text("Cards", font=('Proxima Nova', 50))],
   [sg.Button("View my sets", key="-SETS-", size=(40, 2), font=("Proxima Nova", 14)), sg.Button("About", size=(40, 2),
                                                                                                font=(
                                                                                                "Proxima Nova", 14)),
    sg.Button("Logout", key="-LOGOUT-", size=(40, 2), font=("Proxima Nova", 14))],
   [sg.Text("nAt Cards, we believe that anyone can learn anything. All it takes is a tenacious spirit, the right "
            "guidance and the tools to see it through. "
            "We know that students are under more pressure than ever. It can leaventhem feeling overwhelmed, "
            "produce anxiety and make it all too easy to burn "
            "out. It’s our job to give every student the tools and confidence to succeed, no matter their motivation "
            "or what they’re striving tonachieve.",
            font=("Proxima Nova", 14))],

]

layout2 = [ #LOGIN PAGE
   [sg.Text('Login', font=('Proxima Nova', 50))],
   [sg.Text(''
            '', font=('Proxima Nova', 10))],
   [sg.Text("Username:", font=('Proxima Nova', 14)), sg.InputText(size=(30, 1), key="-USERNAME-",
                                                                  font=('Proxima Nova', 10), do_not_clear=False)],
   [sg.Text("Password: ", font=('Proxima Nova', 14)), sg.InputText(size=(30, 1), key="-PASSWORD-",
                                                   font=('Proxima Nova', 10), password_char='*', do_not_clear=False)],
   [sg.Button("Login", size=(20, 1), font=("Proxima Nova", 14), bind_return_key=True)],
   [sg.Text("", key="-LOGIN_MESSAGE-", font=("Proxima Nova", 14))]
]

layout3 = [ #ABOUT PAGE
   [sg.Text('About', font=('Proxima Nova', 50))],
   [sg.Button("View my sets", key="-SETS2-", size=(40, 2), font=("Proxima Nova", 14)),
    sg.Button("Home", key="-HOME-", size=(40, 2), font=("Proxima Nova", 14))],
   [sg.Text("This is the about page.", font=("Proxima Nova", 14))]
]

layout4 = [ #CARD SETS (CREATION)
   [sg.Text("Card sets", font=('Proxima Nova', 50))],
   [sg.Button("Home", key="-HOME2-", size=(40, 2), font=("Proxima Nova", 14)), sg.Button("Logout", key="-LOGOUT2-",
                                                                           size=(40, 2), font=("Proxima Nova", 14))],
   [sg.Text("This is the card customisation window", font=("Proxima Nova", 14))],
   [sg.Button("New card set", key="-NEW-", size=(20, 1), font=("Proxima Nova", 14))],

    [sg.Text()]
]

layout = [
   [sg.Column(layout1, key='-COL1-'), sg.Column(layout2, visible=False, key='-COL2-'),
    sg.Column(layout3, visible=False, key='-COL3-'), sg.Column(layout4, visible=False, key='-COL4-')],
]

window = sg.Window('Cards', layout, size=(1440, 800), resizable=True, finalize=True)
window.Maximize()

layout = 1
while True:
   event, values = window.read()
   print(event, values)
   if event in (None, 'Exit'):
       break
   if event == '-LOGOUT-':
       window[f'-COL{layout}-'].update(visible=False)
       layout = 2
       window[f'-COL{layout}-'].update(visible=True)
   elif event == '-LOGOUT2-':
       window[f'-COL{layout}-'].update(visible=False)
       layout = 2
       window[f'-COL{layout}-'].update(visible=True)
   elif event == 'About':
       window[f'-COL{layout}-'].update(visible=False)
       layout = 3
       window[f'-COL{layout}-'].update(visible=True)
   elif event == '-HOME-':
       window[f'-COL{layout}-'].update(visible=False)
       layout = 1
       window[f'-COL{layout}-'].update(visible=True)
   elif event == '-HOME2-':
       window[f'-COL{layout}-'].update(visible=False)
       layout = 1
       window[f'-COL{layout}-'].update(visible=True)
   elif event == '-SETS-':
       window[f'-COL{layout}-'].update(visible=False)
       layout = 4
       window[f'-COL{layout}-'].update(visible=True)
   elif event == '-SETS2-':
       window[f'-COL{layout}-'].update(visible=False)
       layout = 4
       window[f'-COL{layout}-'].update(visible=True)

   elif event == "Login":
       if values['-USERNAME-'] == "dash12" and values['-PASSWORD-'] == "password":
           window["-LOGIN_MESSAGE-"].update("")
           window[f'-COL{layout}-'].update(visible=False)
           layout = 1
           window[f'-COL{layout}-'].update(visible=True)
       else:
           window["-LOGIN_MESSAGE-"].update("Incorrect username or password, please try again.")

window.close()


**def menu():
    menu_choice = input("Menu:n1: create a new card setn2: quitn")
    if menu_choice == "1":
        create_card_set()
    elif menu_choice == "2":
        quit()
    else:
        menu()
def quit():
    exit()
def create_card_set():
    global card_set
    card_set = open(input("Card Set Title: ")+".txt",'w')
    add_question()
def save_card_set():
    global card_set
    card_set.close()
    print("Your card set has been saved.")
def add_question():
    global card_set
    question = input("Question: ")
    answer_a = input("Answer A: ")
    answer_b = input("Answer B: ")
    answer_c = input("Answer C: ")
    answer_d = input("Answer D: ")
    correct_answer = input("Which answer is correct (type the letter): ")
    card = [question, answer_a, answer_b, answer_c, answer_d, correct_answer]
    print(card)
    card_set.write(str(card)+"n")
    another_question = input("Add another question? y/n: ")
    if another_question == "y":
        add_question()
    else:
        save_card_set()
menu()**

**I want to incorporate the above code into the create card set window. For example, when a user goes clicks the create card set button, they will trigger a set of questions (title of card set, question title, answer options, correct answer and whether or not they want to add another question…) **

Asked By: Dashiell Gardiner

||

Answers:

Maybe you can do it like this, you can add the code for the detail by yourselves.

import json
import PySimpleGUI as sg

sg.theme('DarkBlue4')

frame_layout = [
    [sg.Text('Question'), sg.Input(do_not_clear=False, key='Question', expand_x=True)],
    [sg.Radio('', 'Correct', key='A', default=True), sg.Text('Answer A'), sg.Input(do_not_clear=False, expand_x=True, key='Answer A')],
    [sg.Radio('', 'Correct', key='B'), sg.Text('Answer B'), sg.Input(do_not_clear=False, expand_x=True, key='Answer B')],
    [sg.Radio('', 'Correct', key='C'), sg.Text('Answer C'), sg.Input(do_not_clear=False, expand_x=True, key='Answer C')],
    [sg.Radio('', 'Correct', key='D'), sg.Text('Answer D'), sg.Input(do_not_clear=False, expand_x=True, key='Answer D')],
]
layout =[
    [sg.Text('File to saves'),
     sg.Input(disabled=True, enable_events=True, expand_x=True, key='Filename'),
     sg.FileBrowse(file_types=(("All Text Files", "*.json"),))],
    [sg.Frame("New Question", frame_layout, expand_x=True)],
    [sg.Push(), sg.Button('Add')],
    [sg.Multiline(size=(40, 10), disabled=True, autoscroll=True, horizontal_scroll=True, expand_x=True, key='Questions')],
    [sg.Push(), sg.Button('Save'), sg.Button('Exit')],
]

window = sg.Window('Question', layout, enable_close_attempted_event=True)
sg.theme('DarkBlue3')

filename, questions = None, []

while True:

    event, values = window.read()

    if event in (sg.WINDOW_CLOSE_ATTEMPTED_EVENT, 'Exit'):
        if sg.popup_yes_no('Are your sure to exit ?') == 'Yes':
            break
    elif event == 'Add':
        record = [values[key] for key in ('Question', 'Answer A', 'Answer B', 'Answer C', 'Answer D')]
        correct = 'ABCD'[[values[key] for key in 'ABCD'].index(True)]
        record.append(correct)
        questions.append(record)
        window['Questions'].update('n'.join(map(str, record))+'nn', append=True)

window.close()
Answered By: Jason Yang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.