将数据插入 ttk.treeview 小部件到特定列 [英] Insert data into ttk.treeview widget to specific column

查看:47
本文介绍了将数据插入 ttk.treeview 小部件到特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据插入到我的 ttk 树视图小部件的特定列中.用户唯一的输入是扫描仪,因此我必须解析每个代码以确定它进入哪个列.现在,parseData() 函数在按下 Enter 时运行,所以我想处理那里的插入.

I'd like to insert data into a specific column of my ttk treeview widget. The users only input is a scanner, so I have to parse each code to determine which column it goes into. Right now, the parseData() function runs on pressing Enter, so I would like to take care of the insert there.

我曾尝试使用 treeview.set(item, column, new_value),但在插入空白行时它会崩溃.

I have tried using the treeview.set(item, column, new_value), but it falls apart when inserting into a blank row.

class MainGUI:
    def __init__(self, master):
        self.master = master
        self.data_view = ttk.Treeview(master)
        self.data_view['columns'] = ["Date", "Item 1", "Item 2", "Item 3", "Item 4", "Source", "Destination", "Cart #",]
        self.data_view['show'] = 'headings'
        self.data_view.heading("Date", text="Date")
        self.data_view.heading("Item 1", text="Item 1")
        self.data_view.heading("Item 2", text="Item 2")
        self.data_view.heading("Item 3", text="Item 3")
        self.data_view.heading("Item 4", text="Item 4")
        self.data_view.heading("Source", text="Source")
        self.data_view.heading("Destination", text="Destination")
        self.data_view.heading("Cart #", text="Cart #")
        self.data_view.pack()

        self.scan_entry = ttk.Entry(master)
        self.scan_entry.pack()
        self.scan_entry.bind('<Return>', self.parseEntry)
        self.scan_entry.focus()

        self.close_button = tk.Button(master, text="Close", command=self.adminLogout, width=15, height=5)
        self.close_button.pack(anchor='se')

    def parseEntry(self,event):
        #INSERT self.scan_entry.get() into #ITEM 1 column, not date volumn
        #If contains certain characters, maybe insert into Source or Destination
        self.scan_entry.delete(0,'end')

理想情况下,我希望能够将空白树插入到任何列中,或者在某些情况下获取所选内容,然后根据输入更改值.

Ideally, I'd like to be able to insert into the blank tree to any column, or in some cases grab what is selected, then change the value based on input.

推荐答案

您必须在不想填充的列中插入空.

You will have to insert empty in the columns you do not want to fill.

并且您可以使用 in 来检查您在 self.scan_entry.get() 中寻找的字符:

And you can use in to check for the character you seek in self.scan_entry.get():

from tkinter import *
import tkinter.ttk as ttk
class MainGUI:
    def __init__(self, master):
        self.master = master
        self.data_view = ttk.Treeview(master)
        self.data_view['columns'] = ["Date", "Item 1", "Item 2", "Item 3", "Item 4", "Source", "Destination", "Cart #",]
        self.data_view['show'] = 'headings'
        self.data_view.heading("Date", text="Date")
        self.data_view.heading("Item 1", text="Item 1")
        self.data_view.heading("Item 2", text="Item 2")
        self.data_view.heading("Item 3", text="Item 3")
        self.data_view.heading("Item 4", text="Item 4")
        self.data_view.heading("Source", text="Source")
        self.data_view.heading("Destination", text="Destination")
        self.data_view.heading("Cart #", text="Cart #")
        self.data_view.pack()
        self.scan_entry = ttk.Entry(master)
        self.scan_entry.pack()
        self.scan_entry.bind('<Return>', self.parseEntry)
        self.scan_entry.focus()

        self.close_button = ttk.Button(master, text="Close", command=lambda:print('want to close app'))
        self.close_button.pack(anchor='se')

    def parseEntry(self,event):
        #self.scan_entry.delete(0,'end')
        self.data_view.insert('',END,values=('',self.scan_entry.get(),'','','','','',''))
        #If contains certain characters, maybe insert into Source or Destination       
        if 'a' in self.scan_entry.get():
            self.data_view.insert('',END,values=('','','','','',self.scan_entry.get(),self.scan_entry.get(),''))


if __name__=='__main__':
    master=Tk()
    maingui=MainGUI(master)
    master.mainloop()

这篇关于将数据插入 ttk.treeview 小部件到特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆