How to save information from the program then use it to show in program again (simple programming)

和自甴很熟 提交于 2021-01-29 17:35:57

问题


I have a user-interface, I made. It has Entry form and Treeview section. When I entry information, it show on the treeview (table with 4 column). But when I close the built gui, Those information disappear then when I build again no information show in treeview. I don't want to send information to data server or any clouds, I need save it in my local drive or local folder that this program locate.

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

These is a simple program that I created for my personal life. If you have some suggestions to this concept, please leave it below


回答1:


Try it using pickle.

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



回答2:


Add following code at the end of addit function (move imports at the top of the module):

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)

and create another function:

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)

and call it before mainloop call.



来源:https://stackoverflow.com/questions/56780531/how-to-save-information-from-the-program-then-use-it-to-show-in-program-again-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!