Search code examples
pythontkintertreeviewtk-toolkit

I'm importing a module to another module to make a treeview in tkinter but it is showing me a error NameError: name 'treeview' is not defined


I am trying to import a function add() from module tab.py to a module treeimport.py to create a treeview widget after taking data from user in module treeimport.py and insert that data in the treeview widget im module tab.py after a button ADD is clicked having function add() but it is showing me a error NameError: name 'treeview' is not defined

The module tab.py is below

from tkinter import ttk
from tkinter import *
window = Tk()

def tree():
    col = ('Website','Email','Password','Security check')
    treeview = ttk.Treeview(window, height=5, show='headings', columns=col)

    treeview.column('Website', width=100, anchor=CENTER)
    treeview.column('Email', width=100, anchor=CENTER)
    treeview.column('Password', width=100, anchor=CENTER)
    treeview.column('Security check', width=100, anchor=CENTER)

    treeview.heading('Website', text='Website')
    treeview.heading('Email', text='Email')
    treeview.heading('Password', text='Password')
    treeview.heading('Security check', text='Security check')

    treeview.pack(side=TOP, fill=BOTH)

def add():
    treeview.insert('', 'end',values=(website.get(), email.get(), passwd.get(), 'YES'))

window.mainloop() 

And treeimport module is below:

from tkinter import ttk 
from tkinter import *
from tab import *

ask = Tk()
website = Entry(ask)
email = Entry(ask)
passwd = Entry(ask)

website.pack()
email.pack()
passwd.pack()

rec = Button(ask,text='ADD', command = add())
rec.pack()

ask.mainloop()

Please help me with this problem.


Solution

  • Here, I have actually reduced the use of importing an external file and compiled it all to a single code, and it works fine. I have used some.txt make sure to create such an empty file before trying this code out.

    from tkinter import ttk 
    from tkinter import *
    
    ask = Tk()
    
    def tree():
        window = Toplevel(ask)
        col = ('Website','Email','Password','Security check')
        treeview = ttk.Treeview(window, height=5, show='headings', columns=col)
    
        treeview.column('Website', width=100, anchor=CENTER)
        treeview.column('Email', width=100, anchor=CENTER)
        treeview.column('Password', width=100, anchor=CENTER)
        treeview.column('Security check', width=100, anchor=CENTER)
    
        treeview.heading('Website', text='Website')
        treeview.heading('Email', text='Email')
        treeview.heading('Password', text='Password')
        treeview.heading('Security check', text='Security check')
    
        treeview.pack(side=TOP, fill=BOTH)    
        
        #opening the list
        open_file = open('some.txt','r')
        lines = open_file.readlines()
        
        #populating the list from the file
        for line in lines:
            treeview.insert('', 'end',values=line)
    
    def store():
        #creating a list of data to be stored in file
        vals = [website.get()+' ',email.get()+' ',passwd.get()+' ','Yes\n']
        lines = open('some.txt','a')
        lines.writelines(vals)
        
        #clearing the entry boxes
        website.delete(0,END)
        email.delete(0,END)
        passwd.delete(0,END)
        
        #setting focus back on first window
        website.focus_force()
    
    website = Entry(ask)
    email = Entry(ask)
    passwd = Entry(ask)
    
    website.pack()
    email.pack()
    passwd.pack()
    
    rec = Button(ask,text='ADD', command=store)
    rec.pack()
    
    view = Button(ask,text='VIEW',command=tree)
    view.pack()
    
    ask.mainloop()
    

    some.txt

    www.google.com something@gmail.com stackoverflow Yes 
    

    Do let me know if any errors or doubts.

    Cheers