如何在Tkinter中总结Treeview的值

时间:2017-01-08 02:18:10

标签: python tkinter sum treeview

我正在开发一个GUI,其中的想法是将一些值添加到树视图(作为计费系统),然后获得所有项目的结果或总和。价格。

A shot of the GUI, and "Generar" button result in the terminal 按钮" Agregar" (加英文)获取条目'数据并将它们添加到treevew;其中一个如果要支付的金额。 现在我想要的东西,并且无法获得,是树形视图中给出的值的总和,当在" Generar"时,树视图中显示的值将显示在树视图下方。 (按英语生成)按钮被按下。

接下来,我得到的代码:

#!/usr/bin/python
#-*- coding:utf-8 -*-
from Tkinter import*
from ttk import Combobox, Treeview
from tkMessageBox import*
import MySQLdb
from controller import *
import math

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

        #INSTANCIAS
        global cc, nombre, pago, ref, cod, desc, valor
        #INSTANCIAS DE LOS WIDGETS
        global e1, e2, e3, e4, e5, tree, l8

        cc = IntVar()
        nombre = StringVar()
        pago = StringVar()
        ref = StringVar()
        cod = StringVar()
        desc = StringVar()
        valor = DoubleVar()

        tbancos = ['Bancolombia', "Banco Bogotá", "Banco Agrario", "Banco Occidente"]

        lupa = PhotoImage(file='img/lupa.png')

        tbanktype = ['Corriente','Ahorro']
        fpago = ['Efectivo','Transferencia']

        resultado = DoubleVar()

        #BUSQUEDA = ["Nombre","CC/Nit"]
        busqueda = StringVar()
        busqueda.trace("w", lambda name, index, mode: buscar())
        dato = StringVar()

        #WIDGETS

        #========================= HEADER ==============================

        self.titleL = Label(self, text="GASTOS", font="bold")
        self.titleL.pack(pady=20, side=TOP)

        #========================== WRAPPER ============================

        self.wrapper = Frame (self)
        self.wrapper.pack(side=LEFT, fill=Y)
        #Esto centro el wrapper
        #self.wrapper.pack(side=LEFT, fill=BOTH, expand=True)

        #======================== BENEFICIARIO =======================

        self.lf1 = LabelFrame(self.wrapper, text="Beneficiario")
        self.lf1.pack(fill=X, ipady=5)

        self.f0 = Frame(self.lf1)
        self.f0.pack(pady=5, fill=X)#-----------------------------------

        l1 = Label(self.f0, text='CC/Nit:')
        l1.pack(side=LEFT)

        e1 = Entry(self.f0, textvariable=cc)
        e1.pack(side=LEFT)

        b0 = Button(self.f0, text='Buscar:', image=lupa, command=buscarB)
        b0.pack(side=LEFT)

        l2 = Label(self.f0, text='Nombre:')
        l2.pack(side=LEFT)
        e2 = Entry(self.f0, textvariable=nombre)
        e2.pack(side=LEFT, fill=X, expand=1)

        self.f1 = Frame(self.lf1)
        self.f1.pack(pady=5, fill=X)#-----------------------------------

        l3 = Label(self.f1, text='Forma de Pago:')
        l3.pack(side=LEFT)
        Cbx = Combobox(self.f1, textvariable=pago, values=fpago, width=15)
        Cbx.set('Efectivo')
        Cbx.pack(side=LEFT)

        l4 = Label(self.f1, text='Ref. Bancaria:')
        l4.pack(side=LEFT)
        e3 = Entry(self.f1, textvariable=ref)
        e3.pack(side=LEFT, fill=X, expand=1)

        b1 = Button(self.f1, text='Buscar:', image=lupa)
        b1.image=lupa
        b1.pack(side=LEFT)

        #======================== CONCEPTO ========================

        self.lf2 = LabelFrame(self.wrapper, text="Concepto")
        self.lf2.pack(fill=X, ipady=5)

        self.f2 = Frame(self.lf2)
        self.f2.pack(pady=5, fill=X)#-------------------------------

        l5 = Label(self.f2, text='Código:')
        l5.pack(side=LEFT)
        e4 = Entry(self.f2, textvariable=cod)
        e4.pack(side=LEFT)

        b2 = Button(self.f2, text='Buscar:', image=lupa, command=buscarC)
        b2.pack(side=LEFT)

        self.f3 = Frame(self.lf2)
        self.f3.pack(pady=5, fill=X)#-------------------------------

        l6 = Label(self.f3, text='Descripción:')
        l6.pack(side=LEFT)
        e5 = Entry(self.f3, textvariable=desc, state=DISABLED)
        e5.pack(side=LEFT, fill=X, expand=1)

        l7 = Label(self.f3, text='Valor:')
        l7.pack(side=LEFT)
        e6 = Entry(self.f3, width=15, textvariable=valor)
        e6.pack(side=LEFT)

        b3 = Button(self.f3, text='Agregar:', command=agregar)
        b3.pack(side=LEFT)

        #-------------------------- TREEVIEW ---------------------------

        self.f4 = Frame(self.wrapper)
        self.f4.pack(pady=5,fill=X)

        tree = Treeview(self.f4, height=4, show="headings", columns=('col1','col2','col3'))
        tree.pack(side=LEFT, fill=X, expand=1)
        tree.column('col1', width=20, anchor='center')
        tree.column('col2', width=200, anchor='center')
        tree.column('col3', width=10, anchor='center')

        tree.heading('col1', text='Código')
        tree.heading('col2', text='Concepto')
        tree.heading('col3', text='Valor')

        scroll = Scrollbar(self.f4,orient=VERTICAL,command=tree.yview)
        tree.configure(yscrollcommand=scroll.set)

        #--------------------------------------------------------------

        self.f5 = Frame(self.wrapper)
        self.f5.pack(pady=5,fill=X)#-------------------

        #RESULT MUST BE SHOWN HERE
        l8 = Label(self.f5, text=resultado, fg="red", bg="white", anchor='e', font="bold, 22", relief= SUNKEN)
        l8.pack(fill=X, side=RIGHT, expand=1)
        #l8.set("link")

        self.fBtn = Frame(self.wrapper)
        self.fBtn.pack()#-------------------------------

        clean = Button(self.fBtn, text='Cancelar', bg='navy', foreground='white', activebackground='red3', activeforeground='white', command=limpiar)
        clean.pack(side=RIGHT)

        update = Button(self.fBtn, text='Actualizar', bg='navy', foreground='white', activebackground='red3', activeforeground='white', state=DISABLED)
        update.pack(side=RIGHT)

        add = Button(self.fBtn, text='Generar', bg='navy', foreground='white', activebackground='red3', activeforeground='white', command=generar)
        add.pack(side=RIGHT)

        #========================= ASIDE ===========================

        self.aside = Frame(self)
        self.aside.pack(side=TOP, fill=BOTH)

        self.wrap1 = Frame(self.aside)
        self.wrap1.pack()

        self.viewer = Label(self.wrap1, text="LISTA DE GASTOS")
        self.viewer.pack()

        scroll = Scrollbar(self.wrap1, orient=VERTICAL)
        scroll.pack(side=RIGHT, fill=Y)

        lb = Listbox(self.wrap1, yscrollcommand=scroll.set, height=20, width=30)
        scroll.config (command=lb.yview)
        lb.pack(fill=BOTH)
        lb.bind("<Double-Button-1>", callback)

        self.wrap2 = Frame(self.aside)
        self.wrap2.pack()

        load = Button(self.wrap2, text='Cargar lista', bg='navy', foreground='white', activebackground='red3', activeforeground='white', command=cargar_lista)
        load.pack(fill=X)

        delete = Button(self.wrap2, text='Borrar', bg='navy', foreground='white', activebackground='red3', activeforeground='white', command=borrar)
        delete.pack(fill=X)

        edit = Button(self.wrap2, text='Modificar', bg='navy', foreground='white', activebackground='red3', activeforeground='white', command=modificar)
        edit.pack(fill=X)

        buscador = Label(self.wrap2, text="Buscar por Número:")
        buscador.pack()
        E = Entry(self.wrap2, textvariable=busqueda, width=24)
        E.pack()
        E.bind("<KeyRelease>", caps)

def cargar_lista():
    try:
        connect.commit()
        display = "SELECT g_num FROM detalles order by g_num;"
        cursor.execute(display)
        registros = cursor.fetchall()
        lb.delete(0, END)
        for item in registros:
            #print item
            num = item[0]
            lb.insert(END, num)
    except:
        showerror("Mensaje", "Ha ocurrido un error")

# NUEVO / CANCELAR
def limpiar():
    tree.delete(*tree.get_children())
    pass

def agregar():
    v1 = cc.get()
    v2 = None
    v3 = cod.get()
    v4 = desc.get()
    v5 = valor.get()
    tree.insert('', 0, values=(v3,v4,v5))

#FUNCTION THAT GIVE THE VALUES
def generar():
    children = tree.get_children()#OBTIENE LOS iid DE LOS ITEMS
    for child in children:
        i = tree.item(child, 'values')[2]#OBTIENE LOS VALORES DE LOS ITEMS
        print i

def borrar():
    pass

def bloquear():
    pass

def callback(event):
    llenar_campos()

def llenar_campos():
    pass

def habilitar():
    pass

def modificar():
    pass

def actualizar():
    pass

def buscar():
    pass

def buscarB():
    connect.commit()
    try:
        v = cc.get()
        sql = "SELECT b_nombre from beneficiarios WHERE b_cc='%d';" % (v)
        cursor.execute(sql)
        query = cursor.fetchone()
        for n in query:
            nombre.set(n)
    except TypeError, e:
        showerror("Error", e)

    except MySQLdb.IntegrityError, e:
        showerror("Error", e)

def buscarC():
    connect.commit()
    try:
        v = cod.get()
        sql = "SELECT cg_nombre from concepto_gastos WHERE cg_cod='%s';" % (v)
        cursor.execute(sql)
        query = cursor.fetchone()
        for n in query:
            desc.set(n)
    except TypeError, e:
        showerror("Error", e)

    except MySQLdb.IntegrityError, e:
        showerror("Error", e)
    except:
         showerror ("Mensaje", "No se encuentra!")

# CONVIERTE LA ENTRADA DE LOS ENTRIES EN MAYÚSCULA
def caps(event):
    pass

代码没有完成,这就是它有这么多信息的原因。但是接下来的功能是什么给了我需要总和的价值:

def generar():
    children = tree.get_children()#OBTIENE LOS iid DE LOS ITEMS
    for child in children:
        i = tree.item(child, 'values')[2]#OBTIENE LOS VALORES DE LOS ITEMS
        print i

顺便说一句,这个文件被另一个(home.py)调用来与它进行交互。

如果有人能帮我解决这个问题,你就可以挽救生命。感谢您的时间,看看这个,以及您可以回答的任何事情。对不起我的英语,如果不好的话。

2 个答案:

答案 0 :(得分:0)

你必须将字符串转换为float然后你可以添加到变量ie。 total

def generar():

    total = 0.0

    for child in tree.get_children():
        total += float(tree.item(child, 'values')[2])

    print total

但即使在agregar中,您也可以使用按钮

来增加total
# create global variables
total = 0.0
# or 
total_var = DoubleVar()

def agregar():
    # inform function to use external/global variable when you use `+=`
    global total

    v1 = cc.get()
    v2 = None
    v3 = cod.get()
    v4 = desc.get()
    v5 = valor.get()

    total += v5
    # or
    total_var.set(total_var.get() + v5)

    tree.insert('', 0, values=(v3,v4,v5))

完整的工作示例

import Tkinter as tk
import ttk

# --- functions ---

def agregar(v3, v4, v5):
    tree.insert('', 0, values=(v3,v4,v5))

def generar():

    total = 0.0

    for child in tree.get_children():
        total += float(tree.item(child, 'values')[2])

    print total

    result['text'] = 'Total: {}'.format(total)

# --- main ---

root = tk.Tk()

tree = ttk.Treeview(root, height=4, show="headings", columns=('col1','col2','col3'))
tree.pack()

tree.heading('col1', text='Código')
tree.heading('col2', text='Concepto')
tree.heading('col3', text='Valor')

add = tk.Button(root, text='Generar', command=generar)
add.pack()

result = tk.Label(root, text='Total: 0')
result.pack()

agregar("1", "AAA", 1.11)
agregar("2", "BBB", 2.22)
agregar("3", "CCC", 3.33)

root.mainloop()

答案 1 :(得分:0)

您获得PY_VAR240的原因是您的代码试图显示对象而不是值。

这里已经有一个答案暗示了这个问题:Python Tkinter Treeview - Iterating 'get_children' output

以下是我从一行中获取值的方法:

rowItem = treeSomeTree.item(itemID)
itemDictionary = rowItem['values']
someVariable = itemDictionary[0] 

注意item id变量表示行项标识符,它可能是也可能不是数字,必须是唯一的。如果未指定,treeview .insert默认为None(iid = None)。

通过将id作为整数值指定为惰性行,您将始终知道您有(或需要得到)哪一行进行汇总。