简体   繁体   English

如何将 Entry 输入数据转换为浮点数 function? 并写入 CSV 文件

[英]How to convert the Entry input data to a float function? and write in on a CSV file

I'm a noob in writing python code.我是编写 python 代码的菜鸟。 Here is where I'm stuck right now.这就是我现在卡住的地方。 The entry data input needs to be converted to the float for calculation the total.录入数据输入需要转换为浮点数才能计算总计。 Also how can I get this data written on a new CSV file?另外,如何将这些数据写入新的 CSV 文件? Sum_total = amount of milk *2.99 + amount of eggs * $2.49 + amount of orange juice * $3.25 + amount of corn flakes * $4.25. Sum_total = 牛奶量 *2.99 + 鸡蛋量 * $2.49 + 橙汁量 * $3.25 + 玉米片量 * $4.25。 Format of new csv file named receipt.csv: The first column should contain the name of each item;名为receipt.csv的新csv文件的格式:第一列应包含每个项目的名称; the second column should contain the number of each item that the customer would like to purchase;第二列应包含客户想要购买的每件商品的数量; and the third column should contain subtotals for how much the customer is spending on each grocery item.第三列应包含客户在每件杂货上花费的小计。 After the rows for each item, your CSV should contain a “spacer” line filled with “--" in each cell. Finally, the bottom row of your CSV should be labeled “Total” and should contain the total amount that the customer needs to pay to Gerry's groceries在每个项目的行之后,您的 CSV 应该在每个单元格中包含一个用“--”填充的“间隔”行。最后,CSV 的底行应该标记为“总计”,并且应该包含客户需要的总金额支付给格里的杂货

import tkinter
import tkinter.messagebox
import csv
root = tkinter.Tk()
root.title("Gerry's groceries checkout")
root.configure( bg = "khaki1")

#Milk
milk_label = tkinter.Label(root, text = "Milk ($2.99):")
milk_label.grid(row = 0, column = 0)
milk_label.configure( bg = "khaki1")
milk_amount = tkinter.Entry(root, width=10)
milk_amount.grid(row = 0, column = 1)

#Eggs
eggs_label = tkinter.Label(root, text = "Eggs ($2.49):")
eggs_label.grid(row = 1, column = 0)
eggs_label.configure (bg = "khaki1")
eggs_amount = tkinter.Entry(root, width = 10)
eggs_amount.grid(row = 1, column = 1)

#Orange Juice
oj_label = tkinter.Label(root, text = "Orange juice ($3.25):")
oj_label.grid( row = 0, column = 2)
oj_label.configure( bg = "khaki1")
oj_amount = tkinter.Entry(root, width = 10) 
oj_amount.grid(row = 0, column = 3)
#Corn Flakes
cf_label = tkinter.Label(root, text = "Corn flakes ($4.25)")
cf_label.grid( row = 1, column = 2)
cf_label.configure( bg = "khaki1")
cf_amount = tkinter.Entry(root, width = 10) 
cf_amount.grid(row = 1, column = 3)

def checkout():
    """Checkout the total price"""
    try:


        total_eggs = eggs_amount * 2.49
        total_oj = oj_amount *3.25
        total_cf = cf_amount *4.25
        total_milk = milk_amount *2.99
        total = total_eggs + total_oj + total_cf + total_milk
        return total

        #Create a CSV file
        with open("receipt.csv", "w") as file:
            writer = csv.writer(file, lineterminator = "\n")
            row1 = ["Milks", "Orange Juice", "Eggs", "Corn flake"]
            row2 = [milk_amount,oj_amount,eggs_amount,cf_amount]
            row3 = [total]
            writer.writerow(row1)
            writer.writerow(row2)
            writer.writerow(row3)
        tkinter.messagebox.showinfo("Checkout complete!", "Eceipt was written to receipt.csv.")

    except:
        tkinter.messagebox.showwarning("Warning!", "Sorry, the receipt could not be written. Please check that only integer values were enterered.")

#Checkout
checkout_button = tkinter.Button(root, text = " Checkout", command = checkout)
checkout_button.configure( bg = "spring green")
checkout_button.grid(row = 0, column = 4)


#Quit
quit_button = tkinter.Button(root, text = " Quit", command = root.destroy)
quit_button.configure( bg = "red2")
quit_button.grid(row = 1, column = 4)



root.mainloop()

The entry data input needs to be converted to the float for calculation the total.录入数据输入需要转换为浮点数才能计算总计。

Before that, you need to call .get() for the entry data;在此之前,您需要调用.get()获取条目数据; then, you can convert the gotten string to a number:然后,您可以将获取的字符串转换为数字:

        total_eggs = int(eggs_amount.get()) * 2.49
        total_oj   = int(  oj_amount.get()) * 3.25
        total_cf   = int(  cf_amount.get()) * 4.25
        total_milk = int(milk_amount.get()) * 2.99

Also how can I get this data written on a new CSV file?另外,如何将这些数据写入新的 CSV 文件?

First, you have to drop the return total line, so that the checkout function gets to the part where you already write to the file.首先,您必须删除return total ,以便结帐 function 到达您已经写入文件的部分。 Then, you just have to write what your assignment states:然后,你只需要写下你的作业状态:

            writer.writerow(["Milks",      milk_amount.get(), total_milk])
            writer.writerow(["Orange Juice", oj_amount.get(), total_oj  ])
            writer.writerow(["Eggs",       eggs_amount.get(), total_eggs])
            writer.writerow(["Corn flake",   cf_amount.get(), total_cf  ])
            writer.writerow(["------------",             "-", "-----"   ])
            writer.writerow(["Total",                     "", total     ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM