【发布时间】:2022-01-02 16:59:11
【问题描述】:
我正在尝试创建一个简单的表单来快速填充 Excel 文件。
最终的 excel 文件中的列是“数字”(输入 ID 号)、“其他详细信息”(如果需要添加特定详细信息,则输入文本),然后是与我正在尝试的对象的属性相关的几列记录,每个都有几个选项。
对于后面的这些列,我看到 PySimpleGUI 中的单选按钮返回 True/False,而我只想将单选按钮的文本保存在单个列中。如何将每个按钮的 True/False 转换为与按钮相关的文本?
解决方法如下:
import PySimpleGUI as sg
import pandas as pd
EXCEL_FILE = 'data_entry.xlsx'
df = pd.read_excel(EXCEL_FILE)
lst = ('English', 'Chinese', 'Japanese', 'Russian')
lst2 = ('English', 'Chinese', 'Japanese', 'Russian')
lst3 = ('English', 'Chinese', 'Japanese', 'Russian')
lst4 = ('English', 'Chinese', 'Japanese', 'Russian')
lst5 = ('English', 'Chinese', 'Japanese', 'Russian')
layout = [
[sg.Input('', key='INPUT 1')],
[sg.Radio(text, "Radio", enable_events=True, key=f"Radio {i}")
for i, text in enumerate(lst)],
[sg.Radio(text, "Radio2", enable_events=True, key=f"Radio2 {i}")
for i, text in enumerate(lst2)],
[sg.Radio(text, "Radio3", enable_events=True, key=f"Radio3 {i}")
for i, text in enumerate(lst3)],
[sg.Radio(text, "Radio4", enable_events=True, key=f"Radio4 {i}")
for i, text in enumerate(lst4)],
[sg.Radio(text, "Radio5", enable_events=True, key=f"Radio5 {i}")
for i, text in enumerate(lst5)],
[sg.Input('', key='INPUT 2')],
[sg.Push(), sg.Button("Go"), sg.Button('Exit')],
]
window = sg.Window("test", layout, finalize=True)
def clear_input():
for key in values:
window[key]('')
return None
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'Go':
"""
radio_value = window[event].TKIntVar.get()
col = radio_value % 1000 # count from 0
row = (radio_value - col) % 100000 # count from 0
container = radio_value // 100000 # count from 1
# print(container, row, col)
"""
radio_value = window['Radio 0'].TKIntVar.get()
text = lst[radio_value % 1000] if radio_value else None
radio_value2 = window['Radio2 0'].TKIntVar.get()
text2 = lst2[radio_value2 % 1000] if radio_value2 else None
radio_value3 = window['Radio3 0'].TKIntVar.get()
text3 = lst2[radio_value3 % 1000] if radio_value3 else None
radio_value4 = window['Radio4 0'].TKIntVar.get()
text4 = lst2[radio_value4 % 1000] if radio_value4 else None
radio_value5 = window['Radio5 0'].TKIntVar.get()
text5 = lst2[radio_value5 % 1000] if radio_value5 else None
record = [values['INPUT 1'], text, text2, text3, text4, text5, values['INPUT 2']]
print(record)
df = df.append(record, ignore_index=True)
df.to_excel(EXCEL_FILE, index=False)
sg.popup('Data saved!')
clear_input()
window.close()
【问题讨论】:
标签: python pysimplegui