Python Forum
Insert csv data rows into a tree widget
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert csv data rows into a tree widget
#3
Apologises, full code follow
import tkinter
import tkinter.font
from tkinter import ttk
import csv
from csv import DictReader
import sys
import os
import subprocess

tree_columns = ("Drawing", "Issue", "Document type")
sonumber = 0

class TFPP:

    def __init__(self):
        self.tree = None
        self._setup_widgets()
        self._build_tree()
     
    def _setup_widgets(self):
        
        #this is the setup & layout for the top part of the application i.e. the textbox and buttons.
        # setting up the 1st frame
        frame1=ttk.Frame()
        frame1.pack(fill='both', expand=False)

        AmpicsLabel = ttk.Label(frame1,justify="left", anchor="s", text=("Ampics Number :"))
        #sonumber = stringvar()
        self.SOentry=ttk.Entry(frame1,justify="left", width=15, textvariable = sonumber)
        Searchbutton = ttk.Button(frame1,text="Search",command='_do_search')
        AmpicsLabel.pack(side="left", padx=5)
        self.SOentry.pack(side="left", padx=5)
        Searchbutton = ttk.Button(frame1,text="Search", command=self.search)
        Searchbutton.pack(side="right", padx=3, pady=1)
        #
        #
        # setting up the 2nd frame
        frame2=ttk.Frame()
        frame2.pack(fill='both', expand=False)
        def _clear_text():
            self.partdesc.delete(0, 'end')
            self.SOentry.delete(0, 'end')
            x = self.dwgtree.get_children()
            for item in x:
                self.dwgtree.delete(item)

        DescLabel = ttk.Label(frame2,justify="left", anchor="sw", text=("Part Description : "))
        self.partdesc = ttk.Entry(frame2,background='#fff',justify="left", width=57)
        Resetbutton = ttk.Button(frame2,text="Reset",command=_clear_text)

        DescLabel.pack(side="left", padx=5)
        self.partdesc.pack(side="left", padx=5,pady=5)
        Resetbutton.pack(side="right", padx=3, pady=1)
        #
        #
        # setting up the 3rd frame
        frame3=ttk.Frame()
        frame3.pack(fill='both', expand=False)
        Quitbutton = ttk.Button(frame3,text="Quit", command=app.destroy)
        Quitbutton.pack(side="right", padx=3, pady=1) 
        #
        #
        # this is the setup & layout for the drawing list part.
        container = ttk.Frame()
        container.pack(fill='both', expand=False)
        self.dwgtree = ttk.Treeview(columns=tree_columns, show="headings")
        vsb = ttk.Scrollbar(orient="vertical", command=self.dwgtree.yview)
        self.dwgtree.grid(column=0, row=0, sticky='nsew', in_=container)
        vsb.grid(column=1, row=0, sticky='ns', in_=container)
        container.grid_columnconfigure(0, weight=1)
        container.grid_rowconfigure(0, weight=1)   

        #
        #
        # configures the layout of the tree
    def _build_tree(self):
        for col in tree_columns:
            self.dwgtree.heading(col,anchor="w", text=col.title(),
                command=lambda c=col: sortby(self.tree, c, 0))

            self.dwgtree.column("Drawing",width=120,anchor="w", stretch="no")
            self.dwgtree.column("Issue",width=75, anchor="w", stretch="no")
            self.dwgtree.column("Document type",anchor="w",width=300)
            self.treeview=self.dwgtree

        #
        #
        # this reads the csv file and loads the data into the tree
    def search(self):
        if self.SOentry.get() != "":
            x = self.dwgtree.get_children()
            for item in x:
                self.dwgtree.delete(item)
            self.partdesc.delete(0,'end')
                
            with open("c:\python\projects\somedwgs.csv") as csvfile:
                    reader = csv.DictReader(csvfile)
                    for row in reader:
                        result=(row['Product'])
                        if self.SOentry.get() == result:
                                descr=(row['PartDesc'])
                                descDwg=(row['Drawings'])
                                descIss=(row['Issues'])
                                descDscr=(row['Documents'])
                                self.partdesc.insert(0,descr)
                                while True:
                                    if descDwg != "":
                                        self.dwgtree.insert("",'end',values=(descDwg,descIss,descDscr))
                                        next(reader)

                                        break
              
      
       
    
if __name__ == "__main__":
    app = tkinter.Tk()
    app.title("Production Drawings")
    app.geometry("550x330")
    app.iconbitmap('telogo.ico')
    tfpp = TFPP()
    app.mainloop()
Reply


Messages In This Thread
RE: Insert csv data rows into a tree widget - by LMP2016 - Nov-09-2017, 10:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 555 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  how do you style data frame that has empty rows. gsaray101 0 555 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 888 May-29-2023, 02:42 PM
Last Post: Nietzsche
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,287 Dec-19-2022, 11:11 PM
Last Post: Stockers
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,997 Dec-12-2022, 08:22 PM
Last Post: jh67
  How to insert different types of data into a function DrData82 0 1,274 Feb-10-2022, 10:41 PM
Last Post: DrData82
  How to read rainfall time series and insert missing data points MadsM 4 2,260 Jan-06-2022, 10:39 AM
Last Post: amdi40
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,660 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Iterating Through Data Frame Rows JoeDainton123 2 2,987 Aug-09-2021, 07:01 AM
Last Post: Pedroski55
Lightbulb [Solved] df.loc: write data in certain rows ju21878436312 1 1,751 Jun-28-2021, 06:49 AM
Last Post: ju21878436312

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020