I have created a simple application where you can enter a word and press a button, and each time you press the button, an image and 2 labels are packed onto the screen, 1 label containing the words you have typed. I want to make it so that if you close the application and reopen it, the images and labels you have placed by pressing the button are still there. How could this be done?
from tkinter import *
import pickle
import calendar
from tkcalendar import *
import calendar
from tkcalendar import *
from datetime import datetime
from datetime import date
from time import strftime
from datetime import timedelta, datetime, date
from ttkthemes import ThemedTk, THEMES
import pickle
self = Tk()
self.title('Storing data')
self.geometry("850x800")
x = 200
y = 250
c = 460
d = 290
e = 325
f = 355
g = 390
h = 420
i = 460
j = 490
LARGE_FONT= ("Verdana", 24)
SMALL_FONT=("Verdana", 12)
def get_text(file):
with open(file, "r") as MyFile:
return MyFile.read()
def edit_text(file, text, img_dir):
with open(file, "w") as MyFile:
MyFile.write(text "\n" "apple.png")
def step(self):
my_progress['value'] = 5
def display():
global x , y , c , d , e , f , g, h, i, j
box_image = PhotoImage(file='apple.png')
panel2 = Label(self, image=box_image, bg='#f7f6f6')
panel2.image = box_image
panel2.place(x=x, y=y)
x=x
y = y 260
#assessment name
n = Label(self, text="", bg="#e0f6fc", font=60)
n.configure(text=assessment_name.get())
n.pack()
#due date
d = Label(self, text="Due:", bg="#e0f6fc", font=SMALL_FONT)
d.pack()
#cal date
c= Label(self, text="", bg="#e0f6fc", font=SMALL_FONT)
c.pack()
button = Button(self, text="place", command=display)
button.pack()
save = Button(self, text="save", command=edit_text)
save.pack()
open_button =Button(self, text="open", command=get_text)
open_button.pack()
edit_text("textfile.txt", "label contents", "apple.png")
assessment_name = Entry(self)
assessment_name.place(relx=0.5, y=220, anchor='center')
global cal2
cal2 = Calendar(self, background="#e0f6fc", disabledbackground="white", bordercolor="light blue", headersbackground="light blue", normalbackground="#e0f6fc", foreground="black", normalforeground='black', headersforeground='white', selectmode="day", year=2021, month=8, day=9)
cal2.place(relx=0.5, y=400, anchor='center')
due_date = Button(self, text="Submit")
due_date.place(relx=0.5, y=510, anchor='center')
self.mainloop()
CodePudding user response:
If you want to save something for later, you can save it to a text file. You can save the text and the directory for the image
First, create a .txt file where you want ( within the same folder as the code is best ). Next, create a function which gets the text from the file:
def get_text(file):
with open(file, "r") as MyFile:
return MyFile.read()
This code will return a string containing what is in the text file. Next, create a function which can edit the file. It is best to do this by deleting it and re-writing it again
def edit_text(file, text, img_dir):
with open(file "w") as MyFile:
MyFile.write(text "\n" img_dir)
When calling this function, it will change the contents of the file to what is inputted. For example:
edit_text("textfile.txt", "label contents", "apple.png")
This would change the contents of "textfile.txt" to:
label contents
apple.png
You can use this to store data for your application. I hope this helped!
CodePudding user response:
While you could save data into a txt, it is much easier imo to store it as JSON. With JSON, you could store a dictionary of values and recall them quickly and easily. You could then take these values and set them to the various parameters within your app.
Python also has a built in JSON library, so all you have to do is import json
. You can find the documentation here