首页 > 解决方案 > 有没有办法在计时器处于活动状态时使用按钮 - 按钮被禁用,但在计时器启动时启用

问题描述

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

这是我尝试使用的代码。单击开始时,计时器会启动,因此要回答问题,但是,我不希望在开始之前能够单击按钮,那么有没有办法在单击后启用按钮?

标签: pythonpysimplegui

解决方案


线程可以同时运行不同部分的代码。通过使用线程,您可以单击选项。试试这段代码,它允许你在时间运行时单击按钮。

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

推荐阅读