'str'对象在python tkinter

问题描述 投票:0回答:1

我的代码遇到问题,该代码成功地将数据从 Excel 文件加载到带有标题的 Tkinter 网格条目中。但是,当我尝试将此数据保存到 Pandas DataFrame 时,我在第 10 行遇到错误。如果您可以查看我的代码和相关错误,并为我提供正确的代码来保存网格条目数据,我将不胜感激到 Pandas DataFrame。

import tkinter as tk
import pandas as pd

def tcompile():
    masterlines = []
    i =1 
    for i in range(total_rows):
            row = []
            for j in range(total_columns):
                    data = my_entries[i*total_columns+j].get()
                    row.append(data)
            masterlines.append(row)
            df3 = pd.DataFrame(masterlines)
            

root = tk.Tk()
df = pd.read_excel('c:/erp/student2.xlsx',header=None)
total_rows = df.shape[0]
total_columns = df.shape[1]
my_entries = []
for i, row in df.iterrows():
    c = 0
    for cell in row:
        if i==0: 
            for k in range(df.shape[1]): tk.Label(root, fg="red", font=("Helvetica", 16)).grid(row=i, column=k)    # run ok fill my df data into lable 
            tk.Label(fg="red", font=("Helvetica", 16),text=cell).grid(row=i, column=c)    # run ok fill my df data into lable 
            c += 1
        else:
            b3 =  tk.Entry(root)
            b3.grid(row=i, column=c)        
            if i==3 and c==2: b3.config(bg="green")
            b3.insert(0,cell)
            my_entries.append(cell)
            c += 1


button = tk.Button(root, text="Click me", command=tcompile)
button.grid(row=1000, columnspan=4)
root.mainloop()

查看错误

  File "c:\mainfol\mfatt\gridentrytesting02010.py", line 10, in tcompile
    data = my_entries[i*total_columns+j].get()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get'

error == 'str' object has no attribute 'get'
python tkinter
1个回答
0
投票

当您在第 10 行(

my_entries
)调用
data = my_entries[i*total_columns+j].get()
时,您正在调用一个字符串而不是
tk.Entry
小部件。在字符串上调用
get()
属性将会给出您所看到的错误。 要解决此问题,请将
tk.Entry
小部件存储在
my_entries
中,因此 else 循环将如下所示:

b3 =  tk.Entry(root)
b3.grid(row=i, column=c)        
if i==3 and c==2: b3.config(bg="green")
    b3.insert(0,cell)
    my_entries.append(cell)
    c += 1
© www.soinside.com 2019 - 2024. All rights reserved.