-1

I'm on Stackoverflow for the first time and I'm from Russia (suddenly it's important). I'm learning Python and creating a small quiz game with a user interface. There are a few problems that I can't solve:

  1. I want to create a 2-page game. When the user clicks the OK button, the first page disappears and the second one appears, and the program writes the user's answers to the d3 dictionary. How do I write the user's answers to this dictionary?
  2. I wrote one sentence on the second page for the test. However, when I click OK, the second page is completely empty.

Here is the code:

import PySimpleGUI as sg

d = {
    1: "1. Когда мы познакомились?\n 1) 4 июля\n 2) 20 июля\n 3) 12 июля\n 4) 13 июля",
    2: "2. Чего я боюсь больше всего в жизни?",
    3: "3. Какой цвет я больше всего предпочитаю?\n 1) розовый\n 2) чёрный\n 3) голубой\n 4) зелёный",
    4: "4. Моё тотемное животное?",
    5: "5. Какая марка автомобилей мне нравится?\n 1) Audi\n 2) Lamborghini\n 3) Tesla\n 4) Nissan",
    6: "6. Мой любимый фрукт летом?\n 1) ананас\n 2) слива\n 3) персик\n 4) инжир",
    7: "7. Я верующий – в кого я верю?(это учёный-физик)",
}  # Вопросы для викторины
d2 = {
    1: 3,
    2: "Клоунов",
    3: 3,
    4: "лиса",
    5: 3,
    6: 3,
    7: "Исаак Ньютон - английский физик, математик, механик и астроном, один из создателей классической физики. Автор фундаментального труда «Математические начала натуральной философии», в котором он изложил закон всемирного тяготения и три закона механики, ставшие основой классической механики.",
}  # правильные ответы
d3 = {}  # Ответы пользователя
font = ("Times New Roman", 14)
sz1 = (30, 3)
sz2 = (110, 3)
sg.set_options(font=font)
sg.theme("DarkAmber")
column1 = [
    [sg.Text("Здравствуйте. Сейчас вы пройдёте небольшую викторину.")],
    [sg.Text(d[1]), sg.InputText()],
    [sg.Text(d[2], size=(sz1)), sg.Multiline(size=(sz2))],
    [sg.Text(d[3]), sg.InputText()],
    [sg.Text(d[4]), sg.InputText()],
    [sg.Text(d[5]), sg.InputText()],
    [sg.Text(d[6]), sg.InputText()],
    [sg.Text(d[7], size=(sz1)), sg.Multiline(size=(sz2))],
    [sg.Button("ОК"), sg.Button("Выйти")],
]  # Первая страница

column2 = [
    [sg.Text("А теперь сравним твои ответы с теми, что подготовил я:", size=(30, 3))],
    [sg.Button("Выйти")],
]  # Вторая страница
layout = [
    [
        sg.Column(
            column1,
            scrollable=True,
            vertical_scroll_only=True,
            visible=True,
            key="column1",
            size=(1366, 768),
        )
    ],
    [
        sg.Column(
            column2,
            scrollable=True,
            vertical_scroll_only=True,
            visible=False,
            key="column2",
            size=(1366, 768),
        )
    ],
]
window = sg.Window("Приветик", layout, finalize=True)
window.Maximize()
column1, column2 = window["column1"], window["column2"]
while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, "Выйти"):
        break
    elif event == "ОК":
        column2.update(visible=True)
        column1.update(visible=False)
window.close()
AKX
  • 152,115
  • 15
  • 115
  • 172
RizeD
  • 1

1 Answers1

0

Two questions here

  1. How do I write the user's answers to this dictionary?

User's answers are in dictionary values, you may specified the option key for all elements, like 1 ~ 7 here for each input elements, after it, value of values is for the dictionary d3 if there's no other element with values read into values, try deepcopy for d3.

  1. the second page is completely empty.

The space for page 1 will still be occupied if visible=False and nothing in the row of page 1. Two ways can solve this issue.

  • Use helper function sg.pin in your layout, it will put a small element in the row.
layout = [
    [
        sg.pin(sg.Column(
            column1,
            scrollable=True,
            vertical_scroll_only=True,
            visible=True,
            key="column1",
            size=(1366, 768),
        ))
    ],
    [
        sg.pin(sg.Column(
            column2,
            scrollable=True,
            vertical_scroll_only=True,
            visible=False,
            key="column2",
            size=(1366, 768),
        ))
    ],
]
  • Put both sg.Column into same row
layout = [
    [
        sg.Column(
            column1,
            scrollable=True,
            vertical_scroll_only=True,
            visible=True,
            key="column1",
            size=(1366, 768),
        ),
        sg.Column(
            column2,
            scrollable=True,
            vertical_scroll_only=True,
            visible=False,
            key="column2",
            size=(1366, 768),
        )
    ],
]
Jason Yang
  • 11,284
  • 2
  • 9
  • 23