简体   繁体   中英

How do I add user input to a list and total up that list?

I'm running a program that tracks the user's expenses. I've gotten most of it complete but need the most crucial part. When I run the program I get an error message of a1 not being defined. Here's what I have:

class Expense(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        home = Label(self, text = "Home: ")
        home.grid(row = 3, column = 0)
        util = Label(self, text = "Utilities: ")
        util.grid(row = 4, column = 0)
        health = Label(self, text = "Health/Medical: ")
        health.grid(row = 5, column = 0)
        food = Label(self, text = "Food: ")
        food.grid(row = 6, column = 0)
        transport = Label(self, text = "Transportation: ")
        transport.grid(row = 7, column = 0)
        
        a1 = Entry(self).grid(row = 3, column = 1)
        a2 = Entry(self).grid(row = 4, column = 1)
        a3 = Entry(self).grid(row = 5, column = 1)
        a4 = Entry(self).grid(row = 6, column = 1)
        a5 = Entry(self).grid(row = 7, column = 1)
        
        submit = Button(self, text = "Submit", command = self.AddExpenses)
        submit.grid(row = 8, column = 1)

    #Here's where I run an issue.
    def AddExpenses(self):
        expenses = [a1, a2, a3, a4, a5]
        total = 0
        for i in list:
            total = total + i
        print("Your total spent", total)

If you are using classes you need to use instance variables to be able to use that variables throughout the class.

You need to use self keyword infront of all those variables. Like:

class Expense(tk.Frame):
    def __init__(self, parent, controller):
        self.a = 1
        self.b = 2

that's just an example.

You should change the code to this:

class Expense(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.home = Label(self, text = "Home: ")
        self.home.grid(row = 3, column = 0)
        self.util = Label(self, text = "Utilities: ")
        self.util.grid(row = 4, column = 0)
        self.health = Label(self, text = "Health/Medical: ")
        self.health.grid(row = 5, column = 0)
        self.food = Label(self, text = "Food: ")
        self.food.grid(row = 6, column = 0)
        self.transport = Label(self, text = "Transportation: ")
        self.transport.grid(row = 7, column = 0)
        
        self.a1 = Entry(self)
        self.a2 = Entry(self)
        self.a3 = Entry(self)
        self.a4 = Entry(self)
        self.a5 = Entry(self)

        self.a1.grid(row = 3, column = 1)
        self.a2.grid(row = 4, column = 1)
        self.a3.grid(row = 5, column = 1)
        self.a4.grid(row = 6, column = 1)
        self.a5.grid(row = 7, column = 1)
        
        self.submit = Button(self, text = "Submit", command = self.AddExpenses)
        self.submit.grid(row = 8, column = 1)

    #Here's where I run an issue.
    def AddExpenses(self):
        expenses = [
                    self.a1.get(),
                    self.a2.get(),
                    self.a3.get(),
                    self.a4.get(),
                    self.a5.get(),
        ]

        total = 0
        for i in list:
            total = total + i
        print("Your total spent", total)

If you are new to Object Oriented Programming with python you should focus on learning that first, else Tkinter is going to get harder.

A few comments to your code.

  • You should generally use import tkinter as tk instead of from tkinter import * , in order not to pollute your namespace and having potential conflicts between names
  • When defining widgets in tkinter, you can only assign them to a variable before calling pack or grid , as these functions return None
  • You only need to assign a widget to a variable if you wish to reuse it later in your code
  • You cannot use your variables a1, a2, a3, a4, a5 in a new function because that function doesn't know about them (they were defined in another function - the __init__ ). To retrieve them elsewhere, you need to assign them to your class instance through the self object.
  • When you wish to get the value from your entries in the AddExpense function, you need to call the get() method to retrieve them

With that in mind, you could try the code below to get what you want

import tkinter as tk
from tkinter import messagebox


class Expense(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        tk.Label(self, text="Home: ").grid(row=3, column=0)
        tk.Label(self, text="Utilities: ").grid(row=4, column=0)
        tk.Label(self, text="Health/Medical: ").grid(row=5, column=0)
        tk.Label(self, text="Food: ").grid(row=6, column=0)
        tk.Label(self, text="Transportation: ").grid(row=7, column=0)

        self.a1 = tk.Entry(self)
        self.a1.grid(row=3, column=1)

        self.a2 = tk.Entry(self)
        self.a2.grid(row=4, column=1)

        self.a3 = tk.Entry(self)
        self.a3.grid(row=5, column=1)

        self.a4 = tk.Entry(self)
        self.a4.grid(row=6, column=1)

        self.a5 = tk.Entry(self)
        self.a5.grid(row=7, column=1)

        tk.Button(self, text="Submit", command=self.AddExpenses).grid(row=8, column=1)

    def AddExpenses(self):
        expenses = [self.a1.get(), self.a2.get(), self.a3.get(), self.a4.get(), self.a5.get()]
        total = sum(map(float, expenses))
        messagebox.showinfo("Expenses", f"Your total spent is: {total}")
        print(f"Your total spent is {total}")

root = tk.Tk()
exp = Expense(root, None)
exp.pack()
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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