如何确认用户是否按下了pysimplegui中的按钮
How to confirm if user has pressed on a button in pysimplegui
我希望 Predict Risk
按钮仅在之前按下上述按钮之一时才起作用。
ui的代码如下:
layout=[[sg.Text('Choose the Industry')],
[sg.Button("Automobile"),sg.Button("Chemical"),sg.Button("Engineering/Consulting"),
sg.Button("FMCG"),sg.Button("Healthcare/Hospitality"),sg.Button("Infrastructue")],
[sg.Button("IT/Comm/DC"),sg.Button("Manufacturing"),sg.Button("Mines"),
sg.Button("Energy/Oil & Gas"),sg.Button("Pharma"),sg.Button("Retail"),sg.Button("Cement")],
[sg.Text(size=(50,1),key=('loaded'))],
[sg.Text('Enter Observation/Recommendation: ', size =(26, 1)), sg.InputText()],
[sg.Button("Predict Risk")],
[sg.Text(size=(30,1),key=('output'))],
[sg.Text('If the above prediction is correct select \'yes\' else select the correct risk.')],
[sg.Button("Yes"),sg.Button("Low"),sg.Button("Medium")],
#[sg.Text('Select the correct risk: '),sg.Button("Low"),sg.Button("Medium")],
[sg.Text(size=(30,2),key=('trained'))],
[sg.Button("Exit"),sg.Button("Clear Fields")]
]
window=sg.Window("Risk Predictor",layout)
while True:
event, values = window.read()
#obs=values[0]
# End program if user closes window or
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
以上代码不完整。
我希望应用程序的其余部分仅在按下顶部按钮之一时才处于活动状态。
其次,有没有一种方法可以将回车键映射到 Predict Risk
按钮,这样用户只需按回车键即可获得预测结果。
您可以使用变量或属性button.metadata
来记录按钮的状态clicked
。每个按钮的初始值由选项 metadata=False
设置,在 sg.Button
表示尚未单击。当按钮被点击时,button.metadata
会被设置为True
,表示点击了这个按钮。
这里展示在button.metadata中记录点击状态的方法。
import PySimpleGUI as sg
items = [
"Automobile", "Chemical", "Engineering/Consulting", "FMCG",
"Healthcare/Hospitality", "Infrastructue", "IT/Comm/DC", "Manufacturing",
"Mines", "Energy/Oil & Gas", "Pharma", "Retail", "Cement",
]
length = len(items)
size = (max(map(len, items)), 1)
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 11))
column_layout = []
line = []
num = 4
for i, item in enumerate(items):
line.append(sg.Button(item, size=size, metadata=False))
if i%num == num-1 or i==length-1:
column_layout.append(line)
line = []
layout = [
[sg.Text('Choose the Industry')],
[sg.Column(column_layout)],
[sg.Text(size=(50,1),key=('loaded'))],
[sg.Text('Enter Observation/Recommendation: ', size =(26, 1)), sg.InputText()],
[sg.Button("Predict Risk", bind_return_key=True)],
[sg.Text(size=(30,1),key=('output'))],
[sg.Text('If the above prediction is correct select \'yes\' else select the correct risk.')],
[sg.Button("Yes"),sg.Button("Low"),sg.Button("Medium")],
[sg.Text(size=(30,2),key=('trained'))],
[sg.Button("Exit"),sg.Button("Clear Fields")]
]
window=sg.Window("Risk Predictor", layout, use_default_focus=False, finalize=True)
for key in window.key_dict: # Remove dash box of all Buttons
element = window[key]
if isinstance(element, sg.Button):
element.block_focus()
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event in items:
window[event].metadata = True
elif event == "Predict Risk" and window["Mines"].metadata:
print("Predict Risk for Mines")
window.close()
我希望 Predict Risk
按钮仅在之前按下上述按钮之一时才起作用。
ui的代码如下:
layout=[[sg.Text('Choose the Industry')],
[sg.Button("Automobile"),sg.Button("Chemical"),sg.Button("Engineering/Consulting"),
sg.Button("FMCG"),sg.Button("Healthcare/Hospitality"),sg.Button("Infrastructue")],
[sg.Button("IT/Comm/DC"),sg.Button("Manufacturing"),sg.Button("Mines"),
sg.Button("Energy/Oil & Gas"),sg.Button("Pharma"),sg.Button("Retail"),sg.Button("Cement")],
[sg.Text(size=(50,1),key=('loaded'))],
[sg.Text('Enter Observation/Recommendation: ', size =(26, 1)), sg.InputText()],
[sg.Button("Predict Risk")],
[sg.Text(size=(30,1),key=('output'))],
[sg.Text('If the above prediction is correct select \'yes\' else select the correct risk.')],
[sg.Button("Yes"),sg.Button("Low"),sg.Button("Medium")],
#[sg.Text('Select the correct risk: '),sg.Button("Low"),sg.Button("Medium")],
[sg.Text(size=(30,2),key=('trained'))],
[sg.Button("Exit"),sg.Button("Clear Fields")]
]
window=sg.Window("Risk Predictor",layout)
while True:
event, values = window.read()
#obs=values[0]
# End program if user closes window or
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
以上代码不完整。
我希望应用程序的其余部分仅在按下顶部按钮之一时才处于活动状态。
其次,有没有一种方法可以将回车键映射到 Predict Risk
按钮,这样用户只需按回车键即可获得预测结果。
您可以使用变量或属性button.metadata
来记录按钮的状态clicked
。每个按钮的初始值由选项 metadata=False
设置,在 sg.Button
表示尚未单击。当按钮被点击时,button.metadata
会被设置为True
,表示点击了这个按钮。
这里展示在button.metadata中记录点击状态的方法。
import PySimpleGUI as sg
items = [
"Automobile", "Chemical", "Engineering/Consulting", "FMCG",
"Healthcare/Hospitality", "Infrastructue", "IT/Comm/DC", "Manufacturing",
"Mines", "Energy/Oil & Gas", "Pharma", "Retail", "Cement",
]
length = len(items)
size = (max(map(len, items)), 1)
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 11))
column_layout = []
line = []
num = 4
for i, item in enumerate(items):
line.append(sg.Button(item, size=size, metadata=False))
if i%num == num-1 or i==length-1:
column_layout.append(line)
line = []
layout = [
[sg.Text('Choose the Industry')],
[sg.Column(column_layout)],
[sg.Text(size=(50,1),key=('loaded'))],
[sg.Text('Enter Observation/Recommendation: ', size =(26, 1)), sg.InputText()],
[sg.Button("Predict Risk", bind_return_key=True)],
[sg.Text(size=(30,1),key=('output'))],
[sg.Text('If the above prediction is correct select \'yes\' else select the correct risk.')],
[sg.Button("Yes"),sg.Button("Low"),sg.Button("Medium")],
[sg.Text(size=(30,2),key=('trained'))],
[sg.Button("Exit"),sg.Button("Clear Fields")]
]
window=sg.Window("Risk Predictor", layout, use_default_focus=False, finalize=True)
for key in window.key_dict: # Remove dash box of all Buttons
element = window[key]
if isinstance(element, sg.Button):
element.block_focus()
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event in items:
window[event].metadata = True
elif event == "Predict Risk" and window["Mines"].metadata:
print("Predict Risk for Mines")
window.close()