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

我正在尝试将函数 add() 从模块 tab.py 导入到模块 treeimport.py 从模块 treeimport.py在单击具有功能 add() 按钮 ADD 后,在树视图小部件 im 模块 tab.py 中插入该数据,但它向我显示错误 NameError: name 'treeview' is not defined

下面是模块tab.py

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

树导入模块如下:

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

请帮我解决这个问题。

这里我其实已经减少了导入外部文件的使用,全部编译成一个代码,效果很好。我已经使用 some.txt 确保在尝试此代码之前创建这样一个空文件。

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 Whosebug Yes 

如果有任何错误或疑问,请告诉我。

干杯

我找到了解决办法。 但是我想把在treeview中添加数据做为后端任务,我正在搜索。

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

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

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

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

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

ask.mainloop()
window.mainloop()