我創建了一個簡單的應用程式,您可以在其中輸入一個單詞并按 a button,每次按button,螢屏上都會顯示一個影像和 2 個標簽,其中 1 個標簽包含您輸入的單詞。我想這樣做,如果您關閉應用程式并重新打開它,您通過按下按鈕放置的影像和標簽仍然存在。這怎么可能?
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()
uj5u.com熱心網友回復:
如果您想保存一些內容以備后用,您可以將其保存到文本檔案中。您可以保存影像的文本和目錄
首先,在您想要的位置創建一個 .txt 檔案(最好在與代碼相同的檔案夾中)。接下來,創建一個從檔案中獲取文本的函式:
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" img_dir)
當呼叫這個函式時,它會將檔案的內容更改為輸入的內容。例如:
edit_text("textfile.txt", "label contents", "apple.png")
這會將“textfile.txt”的內容更改為:
label contents
apple.png
您可以使用它來存盤應用程式的資料。我希望這有幫助!
uj5u.com熱心網友回復:
雖然您可以將資料保存到 txt 中,但將其存盤為 JSON 更容易。使用 JSON,您可以存盤值字典并快速輕松地呼叫它們。然后,您可以獲取這些值并將它們設定為應用程式中的各種引數。
Python 還有一個內置的 JSON 庫,所以你所要做的就是import json. 您可以在此處找到檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/337158.html
