gpt4 book ai didi

Python Tkinter csv读取文件到Entry

转载 作者:行者123 更新时间:2023-12-01 07:15:15 26 4
gpt4 key购买 nike

我正在尝试打开一个 csv 文件,循环遍历并在 Tkinter 输入框中显示 csv 文件中的内容,以便我能够手动编辑然后写入该文件

我尝试将标签更改为条目,但是当我执行任何操作时,条目中均未显示任何内容

def panel_admin():

admingui.destroy()
adminpanel = Toplevel()

with open("Staff Bookings.csv", newline = "") as file:
reader = csv.reader(file)


r = 0
for col in reader:
c = 0
for row in col:

Label(adminpanel, width = 10, height = 2, \
text = row, relief = RIDGE).grid(row = r, column = c)
c += 1
r += 1

最佳答案

您不能像设置标签一样设置条目字段的文本。您需要使用条目小部件的插入方法,如下所示

def panel_admin():

admingui.destroy()
adminpanel = Toplevel()

with open("Staff Bookings.csv", newline = "") as file:
reader = csv.reader(file)


r = 0
for col in reader:
c = 0
for row in col:

curEntry = Entry(adminpanel, width = 10, relief = RIDGE)
curEntry.grid(row = r, column = c)
curEntry.insert(0, row)
c += 1
r += 1

请注意,如果您想写出已编辑的输入字段,您可能应该将 curEntry 存储在列表或字典中,以便稍后访问。例如,您可以使用类似下面的代码将行、列号保留为字典键,并将 Entry 小部件保留为值

fields[(r,c)] = curEntry

关于Python Tkinter csv读取文件到Entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58007675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com