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

发布于 2024-12-03 23:12:35 字数 4024 浏览 0 评论 0原文

我对编程非常陌生,我只是不知道如何让这项工作适合我...

我只是想弄清楚如何将树视图的 col4 中的值提取到一个函数中,该函数允许我在底部的 result_display 标签中提供一个简单的总和。

根据我所读到的内容,我需要在树视图本身上使用 item 方法,但我不知道如何正确引用它。

我尝试过使用各种命令配置将树视图中的任何值转储到测试标签中,但没有一个只产生错误。

基本上,我希望用户能够点击结果按钮,调用 fullcalc 函数,该函数将提取 col4 中的值,将它们相加并使用 endresult 文本变量将该总和转储到 result_display 标签中。

我在这里离基地很远吗?

我很感激你能给我任何可以修改的见解。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-12-10 23:12:35

我是树视图新手,但我想我有你的解决方案。此代码将仅向您展示这些值是如何“打包”的,以便您可以随心所欲地使用它们。

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文