首页 > 解决方案 > 试图让 Tkinter 中的按钮重写 csv 文件

问题描述

想要使我放置的按钮在该人旁边写入/添加一个新列(我已经制作了识别该人的代码)但我试图允许用所有旧信息重写 csv 文件,用户将从 3 个按钮中选择一个附加列来选择填充,这是我迄今为止尝试过的。

#opening the csv file
filepath = 'file name'
csvFile = open(filepath)

#reader through the csv file
reader = csv.reader(csvFile)
# turning the csv file into a list. 
Data = list (reader)
list_of_entries = []

list_of_entries.append(Data["Time"])

def TimeBTN15 ():
    Data["Time"] = '15 mins'
    Data["Time"] = newtime

def TimeBTN30 ():
    Data["Time"] = '30 mins'
    Data["Time"] = newtime

def TimeBTN1 ():
    Data["Time"] = '1 Hour'
    Data["Time"] = newtime

TimeLBL = Label(frame3,text="What time would you like to pick up meal:",font=('Arial',18, "bold"),bg = '#F0EAD6')
TimeLBL.place(x= 80, y= 575)

TimenBTN15 = Button(frame, text = '15 mins', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN15 )
TimenBTN15.place(x= 450, y= 575)

TimenBTN30 = Button(frame, text = '30 min ', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN30 )
TimenBTN30.place(x= 525, y= 575)

TimenBTN1 = Button(frame, text = ' 1 hour', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN1 )
TimenBTN1.place(x= 600, y= 575)

csvFile = open('shopoutput.csv', 'w+')

# writting in the headers 
csvFile.write("Name,Food,Drinks,Price,NewTime\n")
    #adding all the new data into the orginal and new data into the shopoutput csv
for data in list_of_entries:
            
        csvFile.write(data['Name'] + ',')
        csvFile.write(data['Food'] + ',')
        csvFile.write(data['Drinks'] + ',')
        csvFile.write(data['Price'] + ',')
        csvFile.write(data['Time'] + '\n')
          
csvFile.close()

标签: pythoncsvtkinter

解决方案


很难提供帮助,因为您没有提供完整的程序。你在这里有很多问题。 Data是字典列表,而不是字典。您永远不会将任何内容复制到list_of_entries中,因此永远不会有您的旧数据。在用户做出一些选择之前,您不能向 csv 文件写入任何内容——它必须在按钮响应中。这是一个似乎更接近您想要的示例。

#opening the csv file
filepath = 'file name'

with open(filepath) as csvfile:
    csvFile = open(filepath)
    # Read the csv file
    Data = csv.reader(csvFile)

# Add Time to each entry

for row in Data:
    row['Time'] = ''

def writecsv():
    with open('shopoutput.csv', 'w+') as xfile:
        csvFile = csv.DictWriter(xfile, Data[0].keys())
        csvFile.writerows( Data )

def findPerson():
    for row in Data:
        if row['Name'] == targetname:
            return row
    return None

def TimeBTN15 ():
    row = findPerson()
    row["Time"] = '15 mins'
    writecsv()
    sys.exit()

def TimeBTN30 ():
    row = findPerson()
    row["Time"] = '30 mins'
    writecsv()
    sys.exit()

def TimeBTN1 ():
    row = findPerson()
    row["Time"] = '1 Hour'
    writecsv()
    sys.exit()

TimeLBL = Label(frame3,text="What time would you like to pick up meal:",font=('Arial',18, "bold"),bg = '#F0EAD6')
TimeLBL.place(x= 80, y= 575)

TimenBTN15 = Button(frame, text = '15 mins', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN15 )
TimenBTN15.place(x= 450, y= 575)

TimenBTN30 = Button(frame, text = '30 min ', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN30 )
TimenBTN30.place(x= 525, y= 575)

TimenBTN1 = Button(frame, text = ' 1 hour', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN1 )
TimenBTN1.place(x= 600, y= 575)

推荐阅读