Pergunta

I'm very new to programming and I just can't figure out how to make this work for me...

I'm just trying to figure out how to extract the values from the treeview's col4 into a function that would allow me to provide a simple sum in the result_display label at the bottom.

From what I've read, I need to use the item method on the treeview itself, but I can't figure out how to reference it properly.

I've tried just dumping ANY value from the treeview into a test label using various command configurations, but none have produced anything more than an error.

Basically, I want the user to be able to hit the results button, to call the fullcalc function which will extract the values in col4, add them up and dump that sum into the result_display label using the endresult textvariable.

am I way off base here?

I'd appreciate any insight you can give me to tinker with.

import tkinter
from tkinter import *
from tkinter import ttk

### PRIMARY WINDOW AND FRAME ELEMENTS
root = Tk()
root.title('Calorie Counter')
entryframe = ttk.Frame(root, padding='10 10 10 10').grid(column=0, row=0)
listframe = ttk.Frame(root, padding='10 10 10 10').grid(column=0, row=3)
resultframe = ttk.Frame(root, padding='10 10 10 10').grid(column=0, row=6)

### GLOBAL VARIABLES
namevar = StringVar()
calentvar = DoubleVar()
volentvar = DoubleVar()
usedentvar = DoubleVar()
calinvar = DoubleVar()
uomvar = StringVar()
endresult = IntVar()


### FUNCTIONS
def itemadd():
    try:
        c0 = namevar.get()
        c2 = usedentvar.get()
        c3 = calinvar.get()
        f1 = volentvar.get()
        f2 = calentvar.get()
        cpv = f2 // f1

        tv.insert("",0,text=c0, values=(c0,cpv,c2,c3))
    except ValueError:
        pass

def itemremove():
    try:
        tv.delete(tv.focus())
    except ValueError:
        pass

def itemupdate(**args):
    try:
        a = calentvar.get()
        b = volentvar.get()
        c = (a / b)
        d = usedentvar.get()
        calinvar.set(c * d)
    except ValueError:
        pass

def fullcalc():
    try:
        pass
    except ValueError:
        pass


### ENTRY FRAME ELEMENTS
# Labels
name_label = ttk.Label(entryframe, text='Item Name').grid(column=0, row=0, columnspan=1)
calpervol_label = ttk.Label(entryframe, text='Calories  /  Volume').grid(column=1, row=0, columnspan=2)
uom_label = ttk.Label(entryframe, text='Unit of Measure').grid(column=3, row=0, columnspan=1)
used_label = ttk.Label(entryframe, text='Amount Used').grid(column=4, row=0, columnspan=1)
incal_label = ttk.Label(entryframe, text='Calories').grid(column=5, row=0, columnspan=1)

# Variable Entry/Display Elements
name_entry = ttk.Entry(entryframe, width=20, textvariable=namevar).grid(column=0, row=1, columnspan=1)
cal_entry = ttk.Entry(entryframe, width=12, textvariable=calentvar).grid(column=1, row=1, columnspan=1, sticky=(E))
vol_entry = ttk.Entry(entryframe, width=12, textvariable=volentvar).grid(column=2, row=1, columnspan=1, sticky=(W))
uom_combo = ttk.Combobox(entryframe, width=10, state='readonly', textvariable=uomvar, values=['grams', 'milliliters', 'teaspoons', 'tablespoons', 'cups']).grid(column=3, row=1, columnspan=1)
used_entry = ttk.Entry(entryframe, width=10, textvariable=usedentvar).grid(column=4, row=1, columnspan=1)
cal_label = ttk.Label(entryframe, width=10, textvariable=calinvar).grid(column=5, row=1, columnspan=1)

# Buttons
add_button = ttk.Button(entryframe, text='Add Item', command=itemadd).grid(column=0, row=2, columnspan=1, sticky=(E))
remove_button = ttk.Button(entryframe, text='Remove Item', command=itemremove).grid(column=1, row=2, columnspan=1, sticky=(W))
update_button = ttk.Button(entryframe, text='Calculate', command=itemupdate).grid(column=5, row=2, columnspan=1)


### LIST FRAME ELEMENTS
tv = ttk.Treeview(listframe, show='headings')
tv["columns"]=("col0", "col1","col2","col3")
tv.column("col0",width=100)
tv.column("col1",width=100,anchor="center" )
tv.column("col2",width=100)
tv.column("col3",width=100)
tv.heading("col0",text="Item")
tv.heading("col1",text="Calories per Volume")
tv.heading("col2",text="Volume Used")
tv.heading("col3",text="Calories Included")
tv.grid(column=0, row=3, columnspan=6)


### RESULT FRAME ELEMENTS
results = ttk.Button(resultframe, text='Calculate Total', command=fullcalc).grid(column=0, row=18)
results_label = ttk.Label(resultframe, text='Total Calories : ').grid(column=3, row=18)
results_display = ttk.Label(resultframe, textvariable=endresult)


### PROGRAM RUN ELEMENTS
root.mainloop()
Foi útil?

Solução

I am new to treeview, but I think I have your solution. This code will just show you how the values are "packaged" so you can use them however you like.

def fullcalc():
   try:
      # get a list of children of the root node
      children = tv.get_children()
      for child in children:
          # For some odd reason the "set" method actually returns the values
          # if you're not actually setting something
          print(tv.set(child))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top