有没有办法在计时器处于活动状态时使用按钮 - 按钮被禁用,但在计时器启动时启用
Is there a way to use buttons whilsts a timer is active - The buttons were disabled, but are enabled when the timer starts
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")
这是我尝试使用的代码。单击开始时,计时器启动,因此问题得到解答,但是,我不希望在开始之前能够单击按钮,所以有没有办法使按钮在单击后能够使用?
线程使 运行 代码的不同部分并发成为可能。通过使用线程,您可以点击选项。尝试此代码,它允许您在时间 运行.
时单击按钮
# 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")
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")
这是我尝试使用的代码。单击开始时,计时器启动,因此问题得到解答,但是,我不希望在开始之前能够单击按钮,所以有没有办法使按钮在单击后能够使用?
线程使 运行 代码的不同部分并发成为可能。通过使用线程,您可以点击选项。尝试此代码,它允许您在时间 运行.
时单击按钮# 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")