简体   繁体   English

如何将用户输入添加到列表并汇总该列表?

[英]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.当我运行程序时,我收到一条未定义 a1 的错误消息。 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.如果您使用的是类,则需要使用实例变量才能在整个 class 中使用该变量。

You need to use self keyword infront of all those variables.您需要在所有这些变量的前面使用self关键字。 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.如果您不熟悉 Object 面向 python 的编程,您应该首先专注于学习,否则Tkinter会变得更难。

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您通常应该使用import tkinter as tk而不是from tkinter import * ,以免污染您的命名空间和名称之间的潜在冲突
  • When defining widgets in tkinter, you can only assign them to a variable before calling pack or grid , as these functions return None在 tkinter 中定义小部件时,您只能在调用packgrid之前将它们分配给变量,因为这些函数返回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__ ).您不能在新的 function 中使用变量a1, a2, a3, a4, a5 ,因为 function 不知道它们(它们在另一个__init__中定义) To retrieve them elsewhere, you need to assign them to your class instance through the self object.要在其他地方检索它们,您需要通过self object 将它们分配给您的 class 实例。
  • When you wish to get the value from your entries in the AddExpense function, you need to call the get() method to retrieve them当您希望从AddExpense function 中的条目中获取值时,您需要调用get()方法来检索它们

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()

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

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