簡體   English   中英

如何從程序中保存信息,然后使用它在程序中再次顯示(簡單編程)

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

我做了一個用戶界面。 它具有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()

這些是我為個人生活創建的簡單程序。 如果您對此概念有任何建議,請留在下面

試試泡菜吧。

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

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)

並在mainloop調用之前調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM