Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good,

I'm trying to sum the values ​​of a column, while inputting it. Since I put a code in the entry and check if it exists and put it in columns in treeview, and I would like to add only the "price" values, but I can't do it, I get the data from the price column, but I can't get if This 5.99 I have entered another 5.99 add up and give me a total, as I add a price.

What I can be doing wrong? or what I have wrong

Any additional information would be appreciated.


Python
def Cesta(self):
		self.conex()

		self.b_codigo = self.s_Codigo.get()

		self.sql3 = "SELECT * FROM productos WHERE codigo = %s"
		self.mycursor.execute(self.sql3,[(self.b_codigo)])
		self.r_codigo = self.mycursor.fetchall()

		self.row3 = [item['nombre'] for item in self.r_codigo]

		if self.s_Codigo.get() == "":
			MessageBox.showinfo("ERROR", "DEBES INTRODUCIR DATOS", icon="error")
		elif self.r_codigo:
			for self.x2 in self.r_codigo:
				print (self.x2["nombre"], self.x2["talla"], self.x2["precio"]+"€")
				self.tree.insert('', 'end', text=self.x2["nombre"], values=(self.x2["talla"],self.x2["precio"]+" €"))

			print(self.x2["fecha"])
			for self.item in self.tree.get_children():
				self.celda = self.tree.set(self.item,"col2")
				self.resultado += self.celda
				print(self.resultado)
		else:
			MessageBox.showinfo("ERROR", "EL CODIGO INTRODUCIDO NO ES CORRECTO", icon="error")

		self.clear_entry()


Error:
Python
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/Users/tomas/Downloads/PROYECTO/main.py", line 205, in Cesta
    self.resultado += self.celda
AttributeError: 'Aplicacion' object has no attribute 'resultado'


these are the columns:
<pre lang="Python">self.tree = ttk.Treeview(self.pagina1,columns=("col1","col2"), height=50)
		self.tree.grid(column=0, row=2, padx=50, pady=100)

		### COLUMNAS ###
		self.tree.column("#0",width=250)
		self.tree.column("col1",width=150, anchor=CENTER)
		self.tree.column("col2",width=150, anchor=CENTER)

		### NOMBRES COLUMNAS ###
		self.tree.heading("#0", text="Articulo", anchor=CENTER)
		self.tree.heading("col1", text="Talla", anchor=CENTER)
		self.tree.heading("col2", text="Precio", anchor=CENTER)


Everything else is going well for me, but the part in which I want to add the results of the price column does not

Python
or self.item in self.tree.get_children():
				self.celda = self.tree.set(self.item,"col2")
				self.resultado += self.celda
				print(self.resultado)


What I have tried:

What am I doing wrong, to be able to add the values ​​of the prices column every time I insert a new product?
Posted
Updated 19-Nov-22 8:10am

You have not defined the resultado variable anywhere. So add the following line at the beginning of your class:
Python
self.resultado = 0

Note: it may be that you need to re-initialize that variable in other methods of your class, so you should check your logic flows.
 
Share this answer
 
Comments
Tomas Sanchez Garcia 18-Nov-22 10:42am    
Before I tried something similar like this, and it gives me an error, now I'm also doing something like this, but I don't know how to fix it, I'm a bit blank

for self.x2 in self.r_codigo:
print (self.x2["nombre"], self.x2["talla"], self.x2["precio"]+"€")
self.tree.insert('', 'end', text=self.x2["nombre"], values=(self.x2["talla"],self.x2["precio"]+" €"))

print(self.x2["fecha"])
for self.item in self.tree.get_children():
self.resultado = 0
self.celda = int(self.tree.set(self.item,"col2"))
self.total = self.resultado + self.celda
print(self.total)
Richard MacCutchan 18-Nov-22 10:46am    
You set resultado to zero but then you do not use it. You use total instead. You need to rethink exactly what you are trying to do here.
Tomas Sanchez Garcia 19-Nov-22 11:49am    
Hi, I've already tried that, so it only shows me the value of the entered item, but it doesn't add it to the previous one.


It shows me the price of the entered item but it does not add the new prices to the previous ones entered.

I can't get the total of the prices entered in the column.


for self.item in self.tree.get_children():
self.total = 0
self.celda = int(self.tree.set(self.item,"col2"))
self.total += self.celda
print(self.total)
Richard MacCutchan 19-Nov-22 12:18pm    
Think about what you are actually trying to do here. If you have a set of values that you need to total then logic is:
SET total = 0
FOREACH item in value_set
DO
    ADD item.value to total
DONE
PRINT(total)

Looking at the pieces of code you have posted above, I cannot see where this code is supposed to be.
Tomas Sanchez Garcia 19-Nov-22 14:10pm    
Hello, I have already managed to solve the error, the new price is already added to the old one, thanks for making me reflect on it.
Hello, I have already managed to solve the error, the new price is already added to the old one, thanks for making me reflect on it.


Python
for self.x2 in self.r_codigo:
				print (self.x2["nombre"], self.x2["talla"], self.x2["precio"]+"€")
				self.tree.insert('', 'end', text=self.x2["nombre"], values=(self.x2["talla"],self.x2["precio"]))

			self.total = 0
			
			for self.item in self.tree.get_children():	
				self.celda = float(self.tree.set(self.item,"col2"))
				self.total+=self.celda
				print(self.total)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900