如何从程序中保存信息,然后使用它再次在程序中显示(简单编程)

时间:2019-06-26 20:37:25

标签: python user-interface tkinter

我创建了一个用户界面。它具有Entry窗体和Treeview部分。当我输入信息时,它会显示在树视图上(带有4列的表)。但是,当我关闭构建的gui时,这些信息消失了,然后当我再次构建时,树形视图中没有显示任何信息。我不想将信息发送到数据服务器或任何云,我需要将其保存在该程序所在的本地驱动器或本地文件夹中。

#
from tkinter import *
from tkinter import ttk
import webbrowser as web

def addit() :
    web = website.get()
    us = user.get()
    pas = password.get()
    et = etc.get()

    info = [web,us,pas,'*'+et]
    tree.insert('','end',values=info)

def deleteit():
    item = tree.selection()
    tree.delete(item)

def contact():
    url='https://bit.ly/'
    web.open(url)

#----------------------------------------------

window = Tk()
window.geometry()
window.title('Password \'s Book')
window.iconbitmap('icon.ico')


menu=Menu(window)
menucontact = Menu(window, tearoff=0)
menucontact.add_command(label='Contact us',command = contact)

menu.add_cascade(label='Contact us',menu=menucontact)
window.config(menu=menu)


#---------------Variables----------------------
website = StringVar()
user = StringVar()
password = StringVar()
etc = StringVar()


#-----------------Frame--------------------------
F1 = LabelFrame(window,text='Password\'s Book')
F1.grid(row=0,column=0,padx=3,pady=3)

#-------Buttons and EntryBlanks-------------------
LF1 =ttk.Label(F1,text='Website')
LF1.grid(row=0,column=0,padx=10,pady=5)
EF1 = ttk.Entry(F1,textvariable=website)
EF1.grid(row=0,column=1,padx=10,pady=5)

LF2 =ttk.Label(F1,text='Username')
LF2.grid(row=1,column=0,padx=10,pady=5)
EF2 = ttk.Entry(F1,textvariable=user)
EF2.grid(row=1,column=1,padx=10,pady=5)

LF3 =ttk.Label(F1,text='Password')
LF3.grid(row=2,column=0,padx=10,pady=5)
EF3= ttk.Entry(F1,textvariable=password)
EF3.grid(row=2,column=1,padx=10,pady=5)

LF4 =ttk.Label(F1,text='etc.')
LF4.grid(row=3,column=0,padx=10,pady=5)
EF4= ttk.Entry(F1,textvariable=etc)
EF4.grid(row=3,column=1,padx=10,pady=5)

B1 = ttk.Button(F1,text='Add',width=18 ,command= addit )
B1.grid(row=4 ,column=1,pady= 5,padx=10)

B2 = ttk.Button(F1,text='Delete',command= deleteit )
B2.grid(row=4 ,column=0,pady= 5,padx=5)

#LF2 = LabelFrame(window,text='This is list')
#LF2.grid(row=5,column=0)


#----------TreeViewlist----------------------
Header =['Website','Username','Password','etc']

tree=ttk.Treeview(window, height=15,column=Header , show='headings')
tree.grid(row=6,column=0,padx=5,pady=5)


#------------Add Header---------------
for col in Header :
    tree.column(col,width=80)
    tree.heading(col,text=col.title())


window.mainloop()

这些是我为个人生活创建的简单程序。如果您对此概念有任何建议,请留在下面

2 个答案:

答案 0 :(得分:2)

用泡菜试一下。

from tkinter import *
from tkinter import ttk, messagebox
import webbrowser as web
import pickle


def addit() :
    web = website.get()
    us = user.get()
    pas = password.get()
    et = etc.get()

    info = [web,us,pas,'*'+et]
    tree.insert('','end',values=info)

def deleteit():
    item = tree.selection()
    tree.delete(item)

def contact():
    url='https://bit.ly/'
    web.open(url)

def on_closing():
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        x=[tree.item(x)['values'] for x in tree.get_children()]
        filehandler = open('data.pickle', 'wb')
        pickle.dump(x,filehandler)
        filehandler.close()
        window.destroy()


#----------------------------------------------

window = Tk()
window.geometry()
window.title('Password \'s Book')
window.iconbitmap('icon.ico')


menu=Menu(window)
menucontact = Menu(window, tearoff=0)
menucontact.add_command(label='Contact us',command = contact)

menu.add_cascade(label='Contact us',menu=menucontact)
window.config(menu=menu)

#---------------Variables----------------------
website = StringVar()
user = StringVar()
password = StringVar()
etc = StringVar()


#-----------------Frame--------------------------
F1 = LabelFrame(window,text='Password\'s Book')
F1.grid(row=0,column=0,padx=3,pady=3)

#-------Buttons and EntryBlanks-------------------
LF1 =ttk.Label(F1,text='Website')
LF1.grid(row=0,column=0,padx=10,pady=5)
EF1 = ttk.Entry(F1,textvariable=website)
EF1.grid(row=0,column=1,padx=10,pady=5)

LF2 =ttk.Label(F1,text='Username')
LF2.grid(row=1,column=0,padx=10,pady=5)
EF2 = ttk.Entry(F1,textvariable=user)
EF2.grid(row=1,column=1,padx=10,pady=5)

LF3 =ttk.Label(F1,text='Password')
LF3.grid(row=2,column=0,padx=10,pady=5)
EF3= ttk.Entry(F1,textvariable=password)
EF3.grid(row=2,column=1,padx=10,pady=5)

LF4 =ttk.Label(F1,text='etc.')
LF4.grid(row=3,column=0,padx=10,pady=5)
EF4= ttk.Entry(F1,textvariable=etc)
EF4.grid(row=3,column=1,padx=10,pady=5)

B1 = ttk.Button(F1,text='Add',width=18 ,command= addit )
B1.grid(row=4 ,column=1,pady= 5,padx=10)

B2 = ttk.Button(F1,text='Delete',command= deleteit )
B2.grid(row=4 ,column=0,pady= 5,padx=5)

#LF2 = LabelFrame(window,text='This is list')
#LF2.grid(row=5,column=0)


#----------TreeViewlist----------------------
Header =['Website','Username','Password','etc']

tree=ttk.Treeview(window, height=15,column=Header , show='headings')
tree.grid(row=6,column=0,padx=5,pady=5)


#------------Add Header---------------
for col in Header :
    tree.column(col,width=80)
    tree.heading(col,text=col.title())

items = []
try:
    filehandler = open('data.pickle', 'rb')
    items = pickle.load(filehandler)
    filehandler.close()
except:
    pass

for item in items:
    tree.insert('','end',values=item)

window.protocol("WM_DELETE_WINDOW", on_closing)
window.mainloop()

答案 1 :(得分:0)

addit函数的末尾添加以下代码(将导入移至模块顶部):

import os
import json

def addit():
    # ...
    with open(os.path.join(os.path.dirname(__file__), "mydata.json"), "a") as data_file:
        json.dump(info, data_file)

并创建另一个功能:

def populate():
    with open(os.path.join(os.path.dirname(__file__), "mydata.json"), "r") as data_file:
        info = json.load(data_file)
    for elem in info:
        tree.insert('','end',values=elem)

并在主循环调用之前调用它。