【发布时间】:2021-09-24 18:49:40
【问题描述】:
我创建了一个如下所示的预测用户界面
代码如下:
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(do_not_clear=False)],
[sg.Button("Predict Risk", bind_return_key=True,metadata=False)],
[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()
#window=sg.Window("Risk Predictor",layout)
while True:
event, values = window.read()
# End program if user closes window or
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == "Mines":
window['Mines'].metadata = True
df=pd.read_csv('./Audit data_Observation & Risk.csv')
#df1=balancing(df)
df1=df
window['loaded'].update("Mines data is loaded.")
obs=values[0]
#if obs!="":
if event == "Predict Risk" and window["Mines"].metadata and values[0]!="":
with open("./sgd.pickle", 'rb') as f:
sgd = pickle.load(f)
window['trained'].update("")
output=output_sample(df1,sgd,values[0])
output1="The Risk prediction is "+output
window['output'].update(output1)
window['Predict Risk'].metadata=True
if event == 'Yes' and window['Predict Risk'].metadata==True:
newy=preprocess_text(output)
retrain(df1,values[0],newy)
window['trained'].update("The model has been trained on new data.")
continue
elif event =='Low' and window['Predict Risk'].metadata==True:
newy="Low"
retrain(df1,values[0],newy)
window['trained'].update("The model has been trained on updated data.")
continue
elif event =='Medium' and window['Predict Risk'].metadata==True:
newy="Medium"
retrain(df1,values[0],newy)
window['trained'].update("The model has been trained on updated data.")
continue
if event=='Clear Fields':
window['loaded'].update("")
window['output'].update("")
window['trained'].update("")
values[0]=''
window[event].metadata=False
window['Predict Risk'].metadata=False
window.close()
无论如何它还没有完成。我想把这个 UI 分成三个部分。
- 当应用程序运行时,只有
Choose the industry部分出现。 - 选择行业后,弹出窗口会打开
Enter the Observation和Predict Risk。 - 当点击
Predict Risk时,另一个弹出窗口显示预测和反馈机制。
所以我的问题是如何制作交互式弹出窗口?
【问题讨论】:
标签: python pysimplegui