简体   繁体   English

python3 - ttk treeview ...将特定列中的值提取到函数中

[英]python3 - ttk treeview… extract values from specific column into a function

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. 我只想弄清楚如何从treeview的col4中提取值到一个函数,这个函数允许我在底部的result_display标签中提供一个简单的求和。

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. 从我读过的内容来看,我需要在treeview本身上使用item方法,但我无法弄清楚如何正确引用它。

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. 基本上,我希望用户能够点击结果按钮,调用fullcalc函数,该函数将提取col4中的值,将它们相加并使用endresult textvariable将该总和转储到result_display标签中。

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

I am new to treeview, but I think I have your solution. 我是treeview的新手,但我想我有你的解决方案。 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))

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

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