有没有办法使用被禁用但随后启用的按钮
Is there a way to use a button that was disabled, but then enabled
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")
这是我尝试使用的代码。单击开始时,计时器启动,因此问题得到解答,但是,我不希望在开始之前能够单击按钮,所以有没有办法使按钮在单击后能够使用?
这需要在主While循环中处理。我无法用计时器进行很好的倒计时,所以我使用了一个普通的计数器变量。您可以使用它来控制问题的数量。
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()
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")
这是我尝试使用的代码。单击开始时,计时器启动,因此问题得到解答,但是,我不希望在开始之前能够单击按钮,所以有没有办法使按钮在单击后能够使用?
这需要在主While循环中处理。我无法用计时器进行很好的倒计时,所以我使用了一个普通的计数器变量。您可以使用它来控制问题的数量。
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()