Есть ли способ использовать кнопки, когда таймер активен - кнопки были отключены, но активируются при запуске таймера - PullRequest
1 голос
/ 29 мая 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")
                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")

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

1 Ответ

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

многопоточность позволяют одновременно запускать разные части кода. с помощью потоковой передачи вы делаете опцию интерактивной. попробуйте этот код, который позволит вам нажать кнопку во время бега.

# for making screen clear
import ctypes
import platform


def make_dpi_aware():
    if int(platform.release()) >= 8:
        ctypes.windll.shcore.SetProcessDpiAwareness(True)
make_dpi_aware()


import PySimpleGUI as sg
from time import time
import threading

def timer(timeleft_time):
    global timeleft,start
    start = time()
    seconds = timeleft_time
    while True:
        current = time()
        timeleft = int(seconds - (current - start))
        #print(timeleft)
        if event == "1" or event == "2" or event == "3" or event == "4":
            timeleft = -1
            break
        elif timeleft >= 0:
            window["t"].Update(value = timeleft)
            window.Refresh()
            if timeleft <= 0:
                sg.popup("no time left")
                window['1'].Update(disabled=True)
                window['2'].Update(disabled=True)
                window['3'].Update(disabled=True)
                window['4'].Update(disabled=True)
                window["Start"].Update(visible=True)
                window.Refresh()
            else:   
                window['1'].Update(disabled=False)
                window['2'].Update(disabled=False)
                window['3'].Update(disabled=False)
                window['4'].Update(disabled=False)
                window["Start"].Update(visible=False)
                window.Refresh()

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['1'].Update(disabled=False)
        window['2'].Update(disabled=False)
        window['3'].Update(disabled=False)
        window['4'].Update(disabled=False)
        window["Start"].Update(visible=False)

        window.Refresh()

        seconds = 6

        timeleft = seconds
        window["t"].Update(timeleft)
        window.refresh()
        timer_thread = threading.Thread(target=timer,args = (timeleft,),daemon=True)
        timer_thread.start()

    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")
...