试图使Tkinter中的按钮重写csv文件

2024-04-27 02:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

想要制作我在人物旁边放置写/添加新栏的按钮(我已经制作了识别人物的代码),但我试图允许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()

1条回答
网友
1楼 · 发布于 2024-04-27 02:18:49

很难帮助你,因为你没有给出完整的计划。这里有很多问题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)

相关问题 更多 >