I want to send an email using python. The contents of the message is from a sql querry placed inside a ttk treeview widget. so how do i select and send the selected items? the code below prints (I001) inside the email.

 # Specifying the from and to addresses

        self.fromaddr = 'remlaproductions@gmail.com'
        self.toaddrs  = 'draedarockstar@gmail.com'
        self.subject = 'EMP Info'

        # Writing the message (this message will appear in the email)



        self.msg =" %s "%(self.tree.selection())


        # Gmail Login
        self.username = 'remlaproductions'
        self.password = 'draedarockstar'


        #Sending Email


        try:
            self.server = smtplib.SMTP('smtp.gmail.com',587)
            self.server.ehlo()
            self.server.starttls()
            self.server.ehlo()
            self.server.login(self.username,self.password)        
            self.server.sendmail(self.fromaddr, self.toaddrs, self.msg)
            messagebox.showinfo(title = "Email Update", message = "Email Sent")
            print(self.tree.selection())
        except:
            messagebox.showinfo(title = "Email Update", message = "Error sending email")
        self.server.quit()

thanks in advance for your help

I figured it out. Here is what i have done. tree.get_children gives you the row id what i did was get the row id from a loop, then use the id withing tree.set(id) to get a dictionary of the values. and format the output from there. Here is the code:

 self.list = " "

        self.info = self.tree.get_children()
        for i in self.info:
            self.info2 = self.tree.set(i)
            for a in self.info2:
                print(a,":",self.info2[a])
                self.list=self.list + a +": "+ self.info2[a]+'\n'

        self.msg ="{} \n" .format(self.list)

The empty string is for emailing purposes. I wanted to send an email of the result without sending the dictionary parenthesis and so forth. This is just a possible solution.

Here is one example that might help ...

""" tk_treeview101.py

see also ...
http://stackoverflow.com/questions/16746387/tkinter-treeview-widget
"""


import os
try:
    # Python2
    import Tkinter as tk
    import ttk
except ImportError:
    # Python3
    import tkinter as tk
    import tkinter.ttk as ttk


class DirectoryBrowser(tk.Frame):
    def __init__(self, master, path):
        self.path = os.path.abspath(path)
        self.entries = {"": self.path}
        self.root = None

        # setup treeview and scrollbars

        tk.Frame.__init__(self, master)
        self.tree = ttk.Treeview(self)

        ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
        xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)

        self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
        self.tree.heading('#0', text='Path', anchor='w')
        self.tree.bind("<<TreeviewSelect>>", self.update_subtree)

        # fill treeview with root dir and the subdirs
        iid = self.insert("", "end", self.path)
        self.tree.focus(iid)
        self.process_directory(iid, self.path)

        # add tree and scrollbars to frame
        self.tree.grid(in_=self, row=0, column=0, sticky="nsew")
        ysb.grid(in_=self, row=0, column=1, sticky="ns")
        xsb.grid(in_=self, row=1, column=0, sticky="ew")

        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)

        self.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.Y)

        # testing ...
        # returns a list of children belonging to last iid
        print(self.tree.get_children(iid))

    def insert(self, parent, index, path, name="", **kwargs):
        """
        add new element to TreeView
        """
        if "text" in kwargs:
            err = "arg 'text' not available"
            raise ValueError(err)

        kwargs["text"] = path
        if name:
            kwargs["text"] = name

        iid = self.tree.insert(parent, index, **kwargs)
        self.entries[iid] = path
        return iid

    def process_directory(self, parent, path, depth=3):
        if depth == 0:
            return
        for p in os.listdir(path):
            abspath = os.path.join(path, p)
            if os.path.isdir(abspath):
                iid = self.insert(parent,
                                  'end',
                                  path=abspath,
                                  name=p,
                                  open=False)
                self.process_directory(iid, abspath, depth-1)

    # Callbacks

    def update_subtree(self, event):
        iid = self.tree.focus()
        path = self.entries[iid]
        print("%s: %s" % (iid, path))
        print(iid in self.entries.keys())
        #self.process_directory(iid, path)


def test():
    root = tk.Tk()
    mypath = "C:/Python34"
    app = DirectoryBrowser(root, path=mypath)
    # testing ...
    print(app.tree.get_children())  #  returns root children    
    app.mainloop()


if __name__ == "__main__":
    test()

I got a chance to play around with your code thank you for sharing really helped my understanding of the treeview widget

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.