Есть ли способ использовать кнопку, которая была отключена, но затем включена - PullRequest
1 голос
/ 26 мая 2020
import PySimpleGUI as sg
from time import time

q1 = [
        [sg.Text("Question 1!"), sg.Text("Time:"), sg.Text(" ", size=(20,1), key="t")],
        [sg.Text("This is where question 1 will be?"), sg.Button("Start")],
        [sg.Button("Option 1", key="1",button_color=("#ffffff","#151515"), disabled=True, enable_events=True), sg.Button("Option 2", key="2",button_color=("#00ff00", "#151515"), disabled=True)],
        [sg.Button("Option 3", key="3",button_color=("#00ffff","#151515"), disabled=True), sg.Button("Option 4", key="4",button_color=("#ff00ff", "#151515"), disabled=True)],
        [sg.Button("Submit"), sg.Button("Next Question"), sg.Button("Skip")]
    ]

window = sg.Window("Question 1",q1)

while True:
    event, values = window.Read()
    if event is None:
        break
    if event == "Start":
        window.FindElement('1').Update(disabled=False)
        window.FindElement('2').Update(disabled=False)
        window.FindElement('3').Update(disabled=False)
        window.FindElement('4').Update(disabled=False)
        window.FindElement("Start").Update(visible=False)
        window.Refresh()

        seconds = 6

        start = time()
        current = time()
        timeleft = seconds

        while timeleft > 0:
            window.FindElement("t").Update(timeleft)
            window.refresh()
            current = time()
            timeleft = int(seconds - (current - start))
            if timeleft == 0:
                sg.popup("no time left")

        if event == "1":
            sg.popup("Test 1")
        elif event == "2":
            sg.popup("Test 2")
        elif event == "3":
            sg.popup("Test 3")
        elif event == "4":
            sg.popup("Test 4")

Это код, с которым я пытался это сделать. При нажатии кнопки start запускается таймер, поэтому вопрос должен быть ответом, однако я не хочу, чтобы кнопки можно было нажимать перед запуском, поэтому есть ли способ включить кнопку, которая будет использоваться после нажатия?

1 Ответ

0 голосов
/ 27 мая 2020

Это нужно решать в основном While l oop. Я не мог вести обратный отсчет с таймером, поэтому использовал обычную переменную счетчика. Вы можете использовать его для контроля количества вопросов.

import PySimpleGUI as sg
from time import time

q1 = [
        [sg.Text("Question 1!"), sg.Text("Time:"), sg.Text(" ", size=(20,1), key="t")],
        [sg.Text("This is where question 1 will be?"), sg.Button("Start")],
        [sg.Button("Option 1", key="1",button_color=("#ffffff","#151515"), disabled=True, enable_events=True), sg.Button("Option 2", key="2",button_color=("#00ff00", "#151515"), disabled=True)],
        [sg.Button("Option 3", key="3",button_color=("#00ffff","#151515"), disabled=True), sg.Button("Option 4", key="4",button_color=("#ff00ff", "#151515"), disabled=True)],
        [sg.Button("Submit"), sg.Button("Next Question"), sg.Button("Skip"),sg.Quit()]
    ]

window = sg.Window("Question 1",q1)

seconds = 6
time_cnt = False

while True:
    event, values = window.Read()
    print(event, values)
    if event in (None,'Quit'):
        break
    elif event == "Start":
        time_cnt = True
        start = time()

    timeleft = seconds
    window.FindElement("t").Update(timeleft)        
    window.refresh()

    if time_cnt:
        seconds -= 1
        if timeleft <= 0:
            sg.popup("no time left")
            window.FindElement('1').Update(disabled=True)
            window.FindElement('2').Update(disabled=True)
            window.FindElement('3').Update(disabled=True)
            window.FindElement('4').Update(disabled=True)
            window.FindElement("Start").Update(visible=True)
            window.Refresh()                
        else:   
            window.FindElement('1').Update(disabled=False)
            window.FindElement('2').Update(disabled=False)
            window.FindElement('3').Update(disabled=False)
            window.FindElement('4').Update(disabled=False)
            window.FindElement("Start").Update(visible=False)
            window.Refresh()


    if event == "1":
        sg.popup("Test 1")
    elif event == "2":
        sg.popup("Test 2")
    elif event == "3":
        sg.popup("Test 3")
    elif event == "4":
        sg.popup("Test 4")

window.close()
...