为什么我不能将输出放入 excel 文件中?

Why can't I put an output in an excel file?

我正在尝试创建一个代码,使用 xlsxwriter 将输入写入 excel 文件,但 xlsx 文件中的输出是这样的:

<function Act_Key at 0x7fbf27008670>

代码如下:

import tkinter as tk
from tkinter import *
import time
import os
import sys
import pathlib
from pathlib import *
import xlsxwriter

#command
def succes():
    workbook = xlsxwriter.Workbook('/home/matteo/Scrivania/Healir/Healir/Healir_PY/bin/files/info.xlsx')
    worksheet = workbook.add_worksheet()
    worksheet.write('A1', 'Nome')
    worksheet.write('B1', 'Act.Key')
    worksheet.write('A2', str(Nome))
    worksheet.write('B2', str(Act_Key))
    workbook.close()
    config.destroy()
    os.system('python3 /home/matteo/Scrivania/Healir/Healir/Healir_PY/bin/project/menu.py')

#var
def Nome():
    global value1
    value1 = e1.get()

def Act_Key():
    global value2
    value2 = e2.get()


#gui
config = Tk()
config.title('Healir')
config.configure(bg='#7b4397')
config.attributes("-fullscreen", True)

#input

tk.Label(config, text="Nome").grid(row=0)
tk.Label(config, text="Codice Seriale").grid(row=1)
e1 = tk.Entry(config)
e2 = tk.Entry(config)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

suc = Button(config, text='Okay', command=succes)
suc.place(x=300, y = 15)

config.mainloop()

xlsx文件创建正确,A1、B1中的文字书写正确。问题仅出在 Tkinter 条目的输入上。

问题是这样的:

    worksheet.write('A2', str(Nome))
    worksheet.write('B2', str(Act_Key))

NomeAct_Key 是函数。获取 str(..) 值只会打印它们的函数 ID:

>>> def foo():
...   print("abc")
... 
>>> foo
<function foo at 0x102951200>
>>> str(foo)
'<function foo at 0x102951200>'

...这是您当前打印到 .xlsx 文件的内容。

您需要调用那些函数:Nome()Act_Key()

    worksheet.write('A2', str(Nome()))
    worksheet.write('B2', str(Act_Key()))

然后,在这些函数中,只需 return Entry 文本:

def Nome():
    return e1.get()

def Act_Key():
    return e2.get()