Python 3.x 特金特日记
大家好,大家都有作业日记,我已经创建了我的日记,但我担心的是,我的教授希望我们在这个文件中做4个日记。请帮助我添加额外的日记里面。我真的没有这个主意。希望这个网站得到我想要的如果你想扩展你的日记项目,最好将日记项目放在外部文件中,并在你的后端库中读取它们。此外,还应该将边界检查放在后端库中。然后,您的主应用程序将不需要因为日志项目的更改而更改 我使用类修改了您的后端(Python 3.x 特金特日记,python-3.x,tkinter,Python 3.x,Tkinter,大家好,大家都有作业日记,我已经创建了我的日记,但我担心的是,我的教授希望我们在这个文件中做4个日记。请帮助我添加额外的日记里面。我真的没有这个主意。希望这个网站得到我想要的如果你想扩展你的日记项目,最好将日记项目放在外部文件中,并在你的后端库中读取它们。此外,还应该将边界检查放在后端库中。然后,您的主应用程序将不需要因为日志项目的更改而更改 我使用类修改了您的后端(PhotoDiaryLib.py)代码,如下所示: from PIL import ImageTk, Image import j
PhotoDiaryLib.py
)代码,如下所示:
from PIL import ImageTk, Image
import json
class PhotoDiary:
def __init__(self, width=500, height=500):
self.imageWidth = width
self.imageHeight = height
self.current = -1
self.diary = None
self.loadDiaries()
def loadDiaries(self):
with open('diaries.json') as f:
self.diaries = json.load(f)
def getDiaryList(self):
return self.diaries.keys()
def getDiaryImage(self, diary):
return self.diaries[diary]['image']
def setDiary(self, diary):
self.diary = diary
self.diaryDetails = self.diaries[diary]['items']
self.current = -1
def getImage(self, imageName):
originalImage = Image.open(imageName)
imageRatio = float(originalImage.size[0]) / float(originalImage.size[1])
if imageRatio > 1:
targetSize=(self.imageWidth, int(self.imageHeight/imageRatio))
else:
targetSize=(int(self.imageWidth*imageRatio), self.imageHeight)
resizedImage = originalImage.resize(targetSize, Image.ANTIALIAS)
return ImageTk.PhotoImage(resizedImage)
def get(self, index):
''' get the diary item at given index '''
if 0 <= index < len(self.diaryDetails):
self.current = index
details, imageName = self.diaryDetails[index]
return details, self.getImage(imageName)
return None, None
# navigation functions
next = lambda self: self.get(self.current+1)
previous = lambda self: self.get(self.current-1)
您可以向JSON文件中添加更多项
然后修改主应用程序以适应后端库的变化:
from tkinter import *
from PIL import ImageTk,Image
from PhotoDiaryLib import PhotoDiary
class PhotoDiaryApp:
def __init__(self):
self.mainWindow = Tk()
self.mainWindow.title("Diary")
self.pd = PhotoDiary(500, 500)
self.current = StringVar()
# diary list
diaryFrame = Frame(self.mainWindow, bg='blue')
for i, item in enumerate(self.pd.getDiaryList()):
imgtk = ImageTk.PhotoImage(file=self.pd.getDiaryImage(item))
btn = Radiobutton(diaryFrame, text=item, image=imgtk, compound=TOP, indicatoron=0, variable=self.current, value=item)
btn.config(command=lambda d=item:self.changeDiary(d))
btn.grid(row=0, column=i, sticky='ew', ipadx=10)
btn.image = imgtk
if i == 0: self.current.set(item)
diaryFrame.pack(fill=X)
# diary detail frame
detailFrame = Frame(self.mainWindow)
detailFrame.pack(fill=BOTH, expand=1)
self.photoFrame = Frame(detailFrame)
self.labelFrame = Frame(detailFrame)
self.controlFrame = Frame(detailFrame)
self.canvasImage = Canvas(self.photoFrame, width=500, height=500)
self.canvasImage.pack()
self.photo = self.canvasImage.create_image(250, 250, image=None)
self.photoDetails = StringVar()
self.photoLabel = Label(self.labelFrame, textvariable=self.photoDetails)
self.photoLabel.pack()
self.previousButton = Button(self.controlFrame, command=self.previousEntry, text="Previous", width=10)
self.previousButton.pack(side='left')
self.previousNext = Button(self.controlFrame, command=self.nextEntry, text="Next", width=10)
self.previousNext.pack(side='left')
self.photoFrame.pack()
self.labelFrame.pack()
self.controlFrame.pack(pady=10)
# show first diary
self.changeDiary(self.current.get())
self.mainWindow.mainloop()
def changeDiary(self, diary):
#print(self.pd.diary, diary)
if self.pd.diary != diary:
print('Changed to diary:', diary)
self.pd.setDiary(diary)
self.nextEntry()
def updateDiary(self, details, image):
''' update photo details and image '''
if details and image:
self.photoDetails.set(details)
self.photoImage = image
self.canvasImage.itemconfig(self.photo, image=image)
def previousEntry(self):
self.updateDiary(*self.pd.previous())
def nextEntry(self):
self.updateDiary(*self.pd.next())
executeWindow = PhotoDiaryApp()
对于stackoverflow来说,这不是一个合适的问题,它太宽泛了。问题应该集中在一个单一的、具体的问题上。“请帮助我在里面添加额外的日记”并不是一个问题,您是在要求我们为您编写代码。@BryanOakley对您提出的问题表示歉意。老实说,我不知道该如何解决这个问题,但我正在寻找想法或任何帮助。您是否尝试过简单地向
diaryDetails
和imageLoc
添加更多数据?@BryanOakley是的,先生,我尝试添加新的diaryDetails1
和imageLoc1
不起作用。“不起作用”有点太模糊了,我们无法提供帮助。谢谢你的代码,我需要的是在我的tkinter中创建4个日记。你知道怎么做吗。因为上面的代码只有一本日记。它非常可爱,我们如何制作像about图片而不是tabs你可以指定image
选项的Checkbutton
。答案已更新。确保图片尺寸不要太大,150x100左右就可以了。哇,亲爱的,振作起来,先生,非常感谢。断然的。
from tkinter import *
from PIL import ImageTk,Image
from PhotoDiaryLib import PhotoDiary
class PhotoDiaryApp:
def __init__(self):
self.mainWindow = Tk()
self.mainWindow.title("Diary")
self.pd = PhotoDiary(500, 500)
self.current = StringVar()
# diary list
diaryFrame = Frame(self.mainWindow, bg='blue')
for i, item in enumerate(self.pd.getDiaryList()):
imgtk = ImageTk.PhotoImage(file=self.pd.getDiaryImage(item))
btn = Radiobutton(diaryFrame, text=item, image=imgtk, compound=TOP, indicatoron=0, variable=self.current, value=item)
btn.config(command=lambda d=item:self.changeDiary(d))
btn.grid(row=0, column=i, sticky='ew', ipadx=10)
btn.image = imgtk
if i == 0: self.current.set(item)
diaryFrame.pack(fill=X)
# diary detail frame
detailFrame = Frame(self.mainWindow)
detailFrame.pack(fill=BOTH, expand=1)
self.photoFrame = Frame(detailFrame)
self.labelFrame = Frame(detailFrame)
self.controlFrame = Frame(detailFrame)
self.canvasImage = Canvas(self.photoFrame, width=500, height=500)
self.canvasImage.pack()
self.photo = self.canvasImage.create_image(250, 250, image=None)
self.photoDetails = StringVar()
self.photoLabel = Label(self.labelFrame, textvariable=self.photoDetails)
self.photoLabel.pack()
self.previousButton = Button(self.controlFrame, command=self.previousEntry, text="Previous", width=10)
self.previousButton.pack(side='left')
self.previousNext = Button(self.controlFrame, command=self.nextEntry, text="Next", width=10)
self.previousNext.pack(side='left')
self.photoFrame.pack()
self.labelFrame.pack()
self.controlFrame.pack(pady=10)
# show first diary
self.changeDiary(self.current.get())
self.mainWindow.mainloop()
def changeDiary(self, diary):
#print(self.pd.diary, diary)
if self.pd.diary != diary:
print('Changed to diary:', diary)
self.pd.setDiary(diary)
self.nextEntry()
def updateDiary(self, details, image):
''' update photo details and image '''
if details and image:
self.photoDetails.set(details)
self.photoImage = image
self.canvasImage.itemconfig(self.photo, image=image)
def previousEntry(self):
self.updateDiary(*self.pd.previous())
def nextEntry(self):
self.updateDiary(*self.pd.next())
executeWindow = PhotoDiaryApp()