【解決方法】ツリービュー列の合計値


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

エラー:

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)

他のすべてはうまくいっていますが、価格列の結果を追加したい部分がうまくいきません

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 am I doing wrong, to be able to add the values ​​of the prices column every time I insert a new product?

解決策 1

を定義していません resultado どこでも可変。 したがって、クラスの先頭に次の行を追加します。

Python
self.resultado = 0

注: クラスの他のメソッドでその変数を再初期化する必要がある場合があるため、ロジック フローを確認する必要があります。

解決策 2

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)

コメント

タイトルとURLをコピーしました