如何在pysimplegui中获取输入值并将其传递给openpyxl

How to get the input value in pysimplegui and pass it to openpyxl

我想获取输入 '-Name-' 的值并将其传递给 openpyxl 中的值

但问题是(value = '-name-') openpyxl 没有接受它。

test.cell(row=8,column=16).value='-Name-'


import PySimpleGUI as sg
from openpyxl import load_workbook, workbook



wb = load_workbook('test.xlsx')
test = wb.active


layout = [[sg.Text('name of employe')],      
         [sg.Text('Name', size=(15,1)), sg.InputText(key='-Name-')],
         [sg.Button('OK'), sg.Button('Exit')]]


test.cell(row=8,column=16).value='-Name-'


window = sg.Window('title', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == 'OK':
        wb.save('test.xlsx')


window.close()


你要保存的不是字符串'-Name-',而是点击'OK'按钮事件后Input元素的内容

import PySimpleGUI as sg
from openpyxl import load_workbook, workbook


wb = load_workbook('test.xlsx')
test = wb.active

layout = [[sg.Text('name of employe')],
         [sg.Text('Name', size=(15,1)), sg.InputText(key='-Name-')],
         [sg.Button('OK'), sg.Button('Exit')]]

window = sg.Window('title', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == 'OK':
        value = values['-Name-']
        test.cell(row=8, column=16).value = value
        wb.save('test.xlsx')

window.close()