Skip to content Skip to sidebar Skip to footer

How To Save Information From The Program Then Use It To Show In Program Again (simple Programming)

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, Th

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

Solution 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.

Post a Comment for "How To Save Information From The Program Then Use It To Show In Program Again (simple Programming)"