{"id":1062514,"date":"2024-12-31T15:52:26","date_gmt":"2024-12-31T07:52:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1062514.html"},"modified":"2024-12-31T15:52:28","modified_gmt":"2024-12-31T07:52:28","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e5%81%9a%e4%b8%80%e4%b8%aa%e7%9b%b8%e5%86%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1062514.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u505a\u4e00\u4e2a\u76f8\u518c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/881ae3e3-eee4-43c2-bce9-02cbc5aefc99.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5229\u7528python\u505a\u4e00\u4e2a\u76f8\u518c\" \/><\/p>\n<p><p> \u5229\u7528Python\u505a\u4e00\u4e2a\u76f8\u518c\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u56fe\u50cf\u5904\u7406\u548c\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillow\uff09\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3001\u4f7f\u7528Tkinter\u6216PyQt5\u8fdb\u884cGUI\u8bbe\u8ba1\u3001\u4f7f\u7528os\u6a21\u5757\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c<\/strong>\u3002\u5176\u4e2d\uff0cPIL\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5904\u7406\u56fe\u50cf\u7684\u663e\u793a\u3001\u7f29\u653e\u3001\u65cb\u8f6c\u7b49\u64cd\u4f5c\uff0c\u800cTkinter\u548cPyQt5\u5219\u53ef\u4ee5\u5e2e\u52a9\u4f60\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5316\u7684\u7528\u6237\u754c\u9762\uff0c\u65b9\u4fbf\u7528\u6237\u6d4f\u89c8\u548c\u7ba1\u7406\u76f8\u518c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u76f8\u518c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u9879\u76ee\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u7ed3\u6784\u6765\u7ec4\u7ec7\u6211\u4eec\u7684\u4ee3\u7801\u548c\u8d44\u6e90\u3002\u4e00\u4e2a\u5178\u578b\u7684\u9879\u76ee\u7ed3\u6784\u53ef\u80fd\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>photo_album\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 images\/<\/p>\n<p>\u2502   \u2514\u2500\u2500 (\u5b58\u653e\u76f8\u518c\u56fe\u7247)<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>\u2514\u2500\u2500 requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>images<\/code>\u6587\u4ef6\u5939\u7528\u4e8e\u5b58\u653e\u76f8\u518c\u7684\u56fe\u7247\uff0c<code>main.py<\/code>\u662f\u6211\u4eec\u7684\u4e3b\u7a0b\u5e8f\u6587\u4ef6\uff0c<code>requirements.txt<\/code>\u7528\u6765\u8bb0\u5f55\u9879\u76ee\u6240\u9700\u7684Python\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5fc5\u8981\u7684Python\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p>pip install tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pillow\u662fPIL\u5e93\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u800cTkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u5f00\u59cb\u7f16\u5199<code>main.py<\/code>\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u5e76\u663e\u793a\u56fe\u50cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageTk<\/p>\n<p>import tkinter as tk<\/p>\n<p>import os<\/p>\n<p>class PhotoAlbum:<\/p>\n<p>    def __init__(self, root, image_dir):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.image_dir = image_dir<\/p>\n<p>        self.image_list = self.load_images()<\/p>\n<p>        self.current_image_index = 0<\/p>\n<p>        self.display_area = tk.Label(root)<\/p>\n<p>        self.display_area.pack()<\/p>\n<p>        self.show_image(self.current_image_index)<\/p>\n<p>    def load_images(self):<\/p>\n<p>        image_list = []<\/p>\n<p>        for file in os.listdir(self.image_dir):<\/p>\n<p>            if file.endswith((&#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;gif&#39;, &#39;bmp&#39;)):<\/p>\n<p>                image_list.append(os.path.join(self.image_dir, file))<\/p>\n<p>        return image_list<\/p>\n<p>    def show_image(self, index):<\/p>\n<p>        image_path = self.image_list[index]<\/p>\n<p>        image = Image.open(image_path)<\/p>\n<p>        image = image.resize((800, 600), Image.ANTIALIAS)<\/p>\n<p>        photo = ImageTk.PhotoImage(image)<\/p>\n<p>        self.display_area.config(image=photo)<\/p>\n<p>        self.display_area.image = photo<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Photo Album&quot;)<\/p>\n<p>    photo_album = PhotoAlbum(root, &quot;images&quot;)<\/p>\n<p>    root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u76f8\u518c\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u52a0\u8f7d\u5e76\u663e\u793a<code>images<\/code>\u6587\u4ef6\u5939\u4e2d\u7684\u56fe\u50cf\u3002\u6211\u4eec\u4f7f\u7528PIL\u5e93\u6765\u5904\u7406\u56fe\u50cf\uff0c\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efaGUI\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u5bfc\u822a\u6309\u94ae<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u65b9\u4fbf\u7528\u6237\u6d4f\u89c8\u76f8\u518c\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u201c\u4e0a\u4e00\u5f20\u201d\u548c\u201c\u4e0b\u4e00\u5f20\u201d\u6309\u94ae\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageTk<\/p>\n<p>import tkinter as tk<\/p>\n<p>import os<\/p>\n<p>class PhotoAlbum:<\/p>\n<p>    def __init__(self, root, image_dir):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.image_dir = image_dir<\/p>\n<p>        self.image_list = self.load_images()<\/p>\n<p>        self.current_image_index = 0<\/p>\n<p>        self.display_area = tk.Label(root)<\/p>\n<p>        self.display_area.pack()<\/p>\n<p>        self.prev_button = tk.Button(root, text=&quot;Previous&quot;, command=self.show_prev_image)<\/p>\n<p>        self.prev_button.pack(side=tk.LEFT)<\/p>\n<p>        self.next_button = tk.Button(root, text=&quot;Next&quot;, command=self.show_next_image)<\/p>\n<p>        self.next_button.pack(side=tk.RIGHT)<\/p>\n<p>        self.show_image(self.current_image_index)<\/p>\n<p>    def load_images(self):<\/p>\n<p>        image_list = []<\/p>\n<p>        for file in os.listdir(self.image_dir):<\/p>\n<p>            if file.endswith((&#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;gif&#39;, &#39;bmp&#39;)):<\/p>\n<p>                image_list.append(os.path.join(self.image_dir, file))<\/p>\n<p>        return image_list<\/p>\n<p>    def show_image(self, index):<\/p>\n<p>        image_path = self.image_list[index]<\/p>\n<p>        image = Image.open(image_path)<\/p>\n<p>        image = image.resize((800, 600), Image.ANTIALIAS)<\/p>\n<p>        photo = ImageTk.PhotoImage(image)<\/p>\n<p>        self.display_area.config(image=photo)<\/p>\n<p>        self.display_area.image = photo<\/p>\n<p>    def show_prev_image(self):<\/p>\n<p>        if self.current_image_index &gt; 0:<\/p>\n<p>            self.current_image_index -= 1<\/p>\n<p>            self.show_image(self.current_image_index)<\/p>\n<p>    def show_next_image(self):<\/p>\n<p>        if self.current_image_index &lt; len(self.image_list) - 1:<\/p>\n<p>            self.current_image_index += 1<\/p>\n<p>            self.show_image(self.current_image_index)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Photo Album&quot;)<\/p>\n<p>    photo_album = PhotoAlbum(root, &quot;images&quot;)<\/p>\n<p>    root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e24\u4e2a\u6309\u94ae\uff0c\u4e00\u4e2a\u662f\u201cPrevious\u201d\uff08\u4e0a\u4e00\u5f20\uff09\uff0c\u53e6\u4e00\u4e2a\u662f\u201cNext\u201d\uff08\u4e0b\u4e00\u5f20\uff09\u3002\u6bcf\u4e2a\u6309\u94ae\u90fd\u6709\u4e00\u4e2a\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u5206\u522b\u7528\u6765\u663e\u793a\u4e0a\u4e00\u5f20\u548c\u4e0b\u4e00\u5f20\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6dfb\u52a0\u56fe\u50cf\u6807\u9898\u548c\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u6bcf\u5f20\u56fe\u7247\u6dfb\u52a0\u6807\u9898\u548c\u63cf\u8ff0\u3002\u5728<code>images<\/code>\u6587\u4ef6\u5939\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>metadata.json<\/code>\u6587\u4ef6\u6765\u5b58\u50a8\u6bcf\u5f20\u56fe\u7247\u7684\u5143\u6570\u636e\u3002<code>metadata.json<\/code>\u6587\u4ef6\u7684\u5185\u5bb9\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>    &quot;image1.jpg&quot;: {<\/p>\n<p>        &quot;title&quot;: &quot;Beautiful Sunset&quot;,<\/p>\n<p>        &quot;description&quot;: &quot;A beautiful sunset over the mountains.&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;image2.jpg&quot;: {<\/p>\n<p>        &quot;title&quot;: &quot;Cityscape&quot;,<\/p>\n<p>        &quot;description&quot;: &quot;A bustling cityscape at night.&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\u6765\u52a0\u8f7d\u5e76\u663e\u793a\u8fd9\u4e9b\u5143\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageTk<\/p>\n<p>import tkinter as tk<\/p>\n<p>import os<\/p>\n<p>import json<\/p>\n<p>class PhotoAlbum:<\/p>\n<p>    def __init__(self, root, image_dir):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.image_dir = image_dir<\/p>\n<p>        self.image_list = self.load_images()<\/p>\n<p>        self.metadata = self.load_metadata()<\/p>\n<p>        self.current_image_index = 0<\/p>\n<p>        self.display_area = tk.Label(root)<\/p>\n<p>        self.display_area.pack()<\/p>\n<p>        self.title_label = tk.Label(root, text=&quot;&quot;, font=(&quot;Helvetica&quot;, 16))<\/p>\n<p>        self.title_label.pack()<\/p>\n<p>        self.description_label = tk.Label(root, text=&quot;&quot;, font=(&quot;Helvetica&quot;, 12))<\/p>\n<p>        self.description_label.pack()<\/p>\n<p>        self.prev_button = tk.Button(root, text=&quot;Previous&quot;, command=self.show_prev_image)<\/p>\n<p>        self.prev_button.pack(side=tk.LEFT)<\/p>\n<p>        self.next_button = tk.Button(root, text=&quot;Next&quot;, command=self.show_next_image)<\/p>\n<p>        self.next_button.pack(side=tk.RIGHT)<\/p>\n<p>        self.show_image(self.current_image_index)<\/p>\n<p>    def load_images(self):<\/p>\n<p>        image_list = []<\/p>\n<p>        for file in os.listdir(self.image_dir):<\/p>\n<p>            if file.endswith((&#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;gif&#39;, &#39;bmp&#39;)):<\/p>\n<p>                image_list.append(os.path.join(self.image_dir, file))<\/p>\n<p>        return image_list<\/p>\n<p>    def load_metadata(self):<\/p>\n<p>        metadata_path = os.path.join(self.image_dir, &quot;metadata.json&quot;)<\/p>\n<p>        if os.path.exists(metadata_path):<\/p>\n<p>            with open(metadata_path, &quot;r&quot;) as f:<\/p>\n<p>                return json.load(f)<\/p>\n<p>        return {}<\/p>\n<p>    def show_image(self, index):<\/p>\n<p>        image_path = self.image_list[index]<\/p>\n<p>        image = Image.open(image_path)<\/p>\n<p>        image = image.resize((800, 600), Image.ANTIALIAS)<\/p>\n<p>        photo = ImageTk.PhotoImage(image)<\/p>\n<p>        self.display_area.config(image=photo)<\/p>\n<p>        self.display_area.image = photo<\/p>\n<p>        image_name = os.path.basename(image_path)<\/p>\n<p>        metadata = self.metadata.get(image_name, {})<\/p>\n<p>        self.title_label.config(text=metadata.get(&quot;title&quot;, &quot;&quot;))<\/p>\n<p>        self.description_label.config(text=metadata.get(&quot;description&quot;, &quot;&quot;))<\/p>\n<p>    def show_prev_image(self):<\/p>\n<p>        if self.current_image_index &gt; 0:<\/p>\n<p>            self.current_image_index -= 1<\/p>\n<p>            self.show_image(self.current_image_index)<\/p>\n<p>    def show_next_image(self):<\/p>\n<p>        if self.current_image_index &lt; len(self.image_list) - 1:<\/p>\n<p>            self.current_image_index += 1<\/p>\n<p>            self.show_image(self.current_image_index)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Photo Album&quot;)<\/p>\n<p>    photo_album = PhotoAlbum(root, &quot;images&quot;)<\/p>\n<p>    root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e24\u4e2a\u6807\u7b7e\u6765\u663e\u793a\u56fe\u7247\u7684\u6807\u9898\u548c\u63cf\u8ff0\u3002\u6211\u4eec\u8fd8\u6dfb\u52a0\u4e86\u4e00\u4e2a<code>metadata.json<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u5b58\u50a8\u6bcf\u5f20\u56fe\u7247\u7684\u5143\u6570\u636e\u3002\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u6d4f\u89c8\u76f8\u518c\u65f6\uff0c\u53ef\u4ee5\u770b\u5230\u6bcf\u5f20\u56fe\u7247\u7684\u6807\u9898\u548c\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u7528\u6237\u6807\u6ce8\u7684\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u76f8\u518c\u66f4\u52a0\u5b9e\u7528\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u529f\u80fd\uff0c\u5141\u8bb8\u7528\u6237\u4e3a\u6bcf\u5f20\u56fe\u7247\u6dfb\u52a0\u6216\u4fee\u6539\u6807\u9898\u548c\u63cf\u8ff0\uff0c\u5e76\u5c06\u8fd9\u4e9b\u4fe1\u606f\u4fdd\u5b58\u5230<code>metadata.json<\/code>\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u76f8\u518c\u754c\u9762\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u6587\u672c\u6846\u548c\u4e00\u4e2a\u4fdd\u5b58\u6309\u94ae\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageTk<\/p>\n<p>import tkinter as tk<\/p>\n<p>import os<\/p>\n<p>import json<\/p>\n<p>class PhotoAlbum:<\/p>\n<p>    def __init__(self, root, image_dir):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.image_dir = image_dir<\/p>\n<p>        self.image_list = self.load_images()<\/p>\n<p>        self.metadata = self.load_metadata()<\/p>\n<p>        self.current_image_index = 0<\/p>\n<p>        self.display_area = tk.Label(root)<\/p>\n<p>        self.display_area.pack()<\/p>\n<p>        self.title_entry = tk.Entry(root, font=(&quot;Helvetica&quot;, 16))<\/p>\n<p>        self.title_entry.pack()<\/p>\n<p>        self.description_entry = tk.Entry(root, font=(&quot;Helvetica&quot;, 12))<\/p>\n<p>        self.description_entry.pack()<\/p>\n<p>        self.save_button = tk.Button(root, text=&quot;Save&quot;, command=self.save_metadata)<\/p>\n<p>        self.save_button.pack()<\/p>\n<p>        self.prev_button = tk.Button(root, text=&quot;Previous&quot;, command=self.show_prev_image)<\/p>\n<p>        self.prev_button.pack(side=tk.LEFT)<\/p>\n<p>        self.next_button = tk.Button(root, text=&quot;Next&quot;, command=self.show_next_image)<\/p>\n<p>        self.next_button.pack(side=tk.RIGHT)<\/p>\n<p>        self.show_image(self.current_image_index)<\/p>\n<p>    def load_images(self):<\/p>\n<p>        image_list = []<\/p>\n<p>        for file in os.listdir(self.image_dir):<\/p>\n<p>            if file.endswith((&#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;gif&#39;, &#39;bmp&#39;)):<\/p>\n<p>                image_list.append(os.path.join(self.image_dir, file))<\/p>\n<p>        return image_list<\/p>\n<p>    def load_metadata(self):<\/p>\n<p>        metadata_path = os.path.join(self.image_dir, &quot;metadata.json&quot;)<\/p>\n<p>        if os.path.exists(metadata_path):<\/p>\n<p>            with open(metadata_path, &quot;r&quot;) as f:<\/p>\n<p>                return json.load(f)<\/p>\n<p>        return {}<\/p>\n<p>    def show_image(self, index):<\/p>\n<p>        image_path = self.image_list[index]<\/p>\n<p>        image = Image.open(image_path)<\/p>\n<p>        image = image.resize((800, 600), Image.ANTIALIAS)<\/p>\n<p>        photo = ImageTk.PhotoImage(image)<\/p>\n<p>        self.display_area.config(image=photo)<\/p>\n<p>        self.display_area.image = photo<\/p>\n<p>        image_name = os.path.basename(image_path)<\/p>\n<p>        metadata = self.metadata.get(image_name, {})<\/p>\n<p>        self.title_entry.delete(0, tk.END)<\/p>\n<p>        self.title_entry.insert(0, metadata.get(&quot;title&quot;, &quot;&quot;))<\/p>\n<p>        self.description_entry.delete(0, tk.END)<\/p>\n<p>        self.description_entry.insert(0, metadata.get(&quot;description&quot;, &quot;&quot;))<\/p>\n<p>    def show_prev_image(self):<\/p>\n<p>        if self.current_image_index &gt; 0:<\/p>\n<p>            self.current_image_index -= 1<\/p>\n<p>            self.show_image(self.current_image_index)<\/p>\n<p>    def show_next_image(self):<\/p>\n<p>        if self.current_image_index &lt; len(self.image_list) - 1:<\/p>\n<p>            self.current_image_index += 1<\/p>\n<p>            self.show_image(self.current_image_index)<\/p>\n<p>    def save_metadata(self):<\/p>\n<p>        image_path = self.image_list[self.current_image_index]<\/p>\n<p>        image_name = os.path.basename(image_path)<\/p>\n<p>        self.metadata[image_name] = {<\/p>\n<p>            &quot;title&quot;: self.title_entry.get(),<\/p>\n<p>            &quot;description&quot;: self.description_entry.get()<\/p>\n<p>        }<\/p>\n<p>        metadata_path = os.path.join(self.image_dir, &quot;metadata.json&quot;)<\/p>\n<p>        with open(metadata_path, &quot;w&quot;) as f:<\/p>\n<p>            json.dump(self.metadata, f, indent=4)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Photo Album&quot;)<\/p>\n<p>    photo_album = PhotoAlbum(root, &quot;images&quot;)<\/p>\n<p>    root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e24\u4e2a\u6587\u672c\u6846\uff0c\u8ba9\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u6216\u4fee\u6539\u56fe\u7247\u7684\u6807\u9898\u548c\u63cf\u8ff0\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e00\u4e2a\u4fdd\u5b58\u6309\u94ae\uff0c\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4fdd\u5b58\u5230<code>metadata.json<\/code>\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u7528\u6237\u5c31\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e3a\u6bcf\u5f20\u56fe\u7247\u6dfb\u52a0\u6216\u4fee\u6539\u6807\u9898\u548c\u63cf\u8ff0\uff0c\u5e76\u5c06\u8fd9\u4e9b\u4fe1\u606f\u4fdd\u5b58\u4e0b\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Python\u76f8\u518c\u5e94\u7528\u7a0b\u5e8f\u3002\u6211\u4eec\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff0c\u4f7f\u7528Tkinter\u5e93\u521b\u5efaGUI\u754c\u9762\uff0c\u5e76\u4f7f\u7528os\u548cjson\u6a21\u5757\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u548c\u6570\u636e\u5b58\u50a8\u3002\u8fd9\u4e2a\u76f8\u518c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u52a0\u8f7d\u5e76\u663e\u793a\u56fe\u7247\uff0c\u5141\u8bb8\u7528\u6237\u6d4f\u89c8\u76f8\u518c\u4e2d\u7684\u56fe\u7247\uff0c\u5e76\u4e3a\u6bcf\u5f20\u56fe\u7247\u6dfb\u52a0\u6216\u4fee\u6539\u6807\u9898\u548c\u63cf\u8ff0\u3002\u867d\u7136\u8fd9\u4e2a\u76f8\u518c\u5e94\u7528\u7a0b\u5e8f\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f88\u597d\u7684\u57fa\u7840\uff0c\u60a8\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\uff0c\u4f8b\u5982\u56fe\u7247\u5206\u7c7b\u3001\u641c\u7d22\u3001\u5e7b\u706f\u7247\u64ad\u653e\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u60a8\u5728Python\u7f16\u7a0b\u7684\u9053\u8def\u4e0a\u53d6\u5f97\u66f4\u5927\u7684\u8fdb\u6b65\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u76f8\u518c\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u76f8\u518c\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684Tkinter\u5e93\u6765\u6784\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002\u4f60\u9700\u8981\u51c6\u5907\u597d\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Pillow\u5e93\u6765\u5904\u7406\u56fe\u50cf\u3002\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\uff0c\u53ef\u4ee5\u52a0\u8f7d\u3001\u663e\u793a\u548c\u6d4f\u89c8\u8fd9\u4e9b\u56fe\u7247\u3002\u53c2\u8003\u4e00\u4e9b\u5728\u7ebf\u6559\u7a0b\u548c\u6587\u6863\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u5feb\u5730\u4e0a\u624b\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u7684Web\u6846\u67b6\u6765\u6784\u5efa\u5728\u7ebf\u76f8\u518c\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u4f7f\u7528Python\u7684Web\u6846\u67b6\u5982Flask\u6216Django\u6765\u521b\u5efa\u5728\u7ebf\u76f8\u518c\u3002\u901a\u8fc7\u8bbe\u7f6e\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u4e0a\u4f20\u56fe\u7247\u3001\u663e\u793a\u76f8\u518c\u9875\u9762\u3001\u5b9e\u73b0\u56fe\u7247\u7684\u6d4f\u89c8\u548c\u4e0b\u8f7d\u529f\u80fd\u3002\u7ed3\u5408HTML\u3001CSS\u548cJavaScript\uff0c\u53ef\u4ee5\u6253\u9020\u4e00\u4e2a\u7f8e\u89c2\u4e14\u529f\u80fd\u4e30\u5bcc\u7684\u5728\u7ebf\u76f8\u518c\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u54ea\u4e9b\u5e93\u6216\u5de5\u5177\u6765\u5b9e\u73b0\u76f8\u518c\u529f\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u5b9e\u73b0\u76f8\u518c\u529f\u80fd\uff0c\u63a8\u8350\u4f7f\u7528Pillow\u5e93\u5904\u7406\u56fe\u7247\uff0cTkinter\u5e93\u7528\u4e8e\u521b\u5efa\u684c\u9762\u5e94\u7528\u7684\u56fe\u5f62\u754c\u9762\u3002\u5982\u679c\u4f60\u9009\u62e9Web\u5f00\u53d1\uff0c\u5219\u9700\u8981\u4f7f\u7528Flask\u6216Django\u4f5c\u4e3a\u540e\u7aef\u6846\u67b6\u3002\u6b64\u5916\uff0c\u524d\u7aef\u53ef\u80fd\u9700\u8981\u4f7f\u7528Bootstrap\u6765\u7f8e\u5316\u9875\u9762\uff0cJavaScript\u53ef\u4ee5\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002\u786e\u4fdd\u5b89\u88c5\u8fd9\u4e9b\u5e93\u5e76\u4e86\u89e3\u5176\u57fa\u672c\u7528\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u987a\u5229\u5730\u5b8c\u6210\u76f8\u518c\u9879\u76ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5229\u7528Python\u505a\u4e00\u4e2a\u76f8\u518c\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u56fe\u50cf\u5904\u7406\u548c\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\uff08Pillo [&hellip;]","protected":false},"author":3,"featured_media":1062516,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062514"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1062514"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062514\/revisions"}],"predecessor-version":[{"id":1062518,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062514\/revisions\/1062518"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1062516"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1062514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1062514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1062514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}