{"id":852,"date":"2023-05-31T00:34:16","date_gmt":"2023-05-30T16:34:16","guid":{"rendered":"http:\/\/xinyixx.com\/?p=852"},"modified":"2023-06-12T00:21:54","modified_gmt":"2023-06-11T16:21:54","slug":"python-ebook","status":"publish","type":"post","link":"https:\/\/www.xinyixx.com\/index.php\/2023\/05\/31\/python-ebook\/","title":{"rendered":"python\u7a0b\u5e8f5\uff1a\u4e00\u4e2a\u7535\u5b50\u4e66\u9605\u8bfb\u5668"},"content":{"rendered":"<p>\u4eca\u5929\u5199\u4e00\u4e2a\u7535\u5b50\u4e66\u9605\u8bfb\u5668\uff0c\u7528\u5230\u7684\u4f9d\u7136\u662ftkinter\u6a21\u5757\uff0c\u5b9e\u73b0\u6587\u672c\u7684\u8bfb\u53d6\uff0c\u9000\u51fa\uff0c\u66f4\u6539\u6587\u5b57\u5927\u5c0f\uff0c\u989c\u8272\uff0c\u63d2\u5165\u4e66\u7b7e\uff0c\u8df3\u8f6c\u4e66\u7b7e\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\">import tkinter as tk\nfrom tkinter import filedialog\nfrom tkinter import messagebox\nfrom tkinter import simpledialog\nimport pickle\n\nclass EbookReader(tk.Frame):\n    def __init__(self, master=None):\n        super().__init__(master)\n        self.master = master\n        self.master.title(\"Ebook Reader\")\n        self.pack()\n\n        self.create_widgets()\n\n    def create_widgets(self):\n        # \u6587\u4ef6\u83dc\u5355\n        menubar = tk.Menu(self.master)\n        filemenu = tk.Menu(menubar, tearoff=0)\n        filemenu.add_command(label=\"Open\", command=self.open_file)\n        filemenu.add_command(label=\"Save\", command=self.save_file)\n        menubar.add_cascade(label=\"File\", menu=filemenu)\n\n        # \u6587\u672c\u6846\n        self.text = tk.Text(self, wrap=\"word\")\n        self.text.pack(expand=True, fill=\"both\")\n\n        # \u4e66\u7b7e\u83dc\u5355\n        bookmarkmenu = tk.Menu(menubar, tearoff=0)\n        bookmarkmenu.add_command(label=\"Add Bookmark\", command=self.add_bookmark)\n        bookmarkmenu.add_command(label=\"Go to Bookmark\", command=self.go_to_bookmark)\n        menubar.add_cascade(label=\"Bookmark\", menu=bookmarkmenu)\n\n        # \u5b57\u4f53\u83dc\u5355\n        fontmenu = tk.Menu(menubar, tearoff=0)\n        fontmenu.add_command(label=\"Increase Font Size\", command=self.increase_font_size)\n        fontmenu.add_command(label=\"Decrease Font Size\", command=self.decrease_font_size)\n        fontmenu.add_command(label=\"Change Font Color\", command=self.change_font_color)\n        menubar.add_cascade(label=\"Font\", menu=fontmenu)\n\n        self.master.config(menu=menubar)\n        <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"># <\/mark><\/code><\/mark><\/code>\u6253\u5f00\u6587\u4ef6<code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\">\n    def open_file(self):\n        filepath = filedialog.askopenfilename()\n        if filepath:\n            with open(filepath, \"r\", encoding=\"utf-8\") as f:\n                self.text.delete(\"1.0\", \"end\")\n                self.text.insert(\"end\", f.read())\n        <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"># <\/mark><\/code><\/mark><\/code>\u4fdd\u5b58\u6587\u4ef6\n    def save_file(self):\n        filepath = filedialog.asksaveasfilename()\n        if filepath:\n            with open(filepath, \"w\", encoding=\"utf-8\") as f:\n                f.write(self.text.get(\"1.0\", \"end\"))\n        <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"># <\/mark><\/code><\/mark><\/code>\u52a0\u5165\u4e66\u7b7e<\/mark><\/code>\n    def add_bookmark(self):\n        bookmark_name = simpledialog.askstring(title=\"Add Bookmark\", prompt=\"Enter bookmark name:\")\n        if bookmark_name:\n            self.text.tag_add(bookmark_name, \"sel.first\", \"sel.last\")\n            bookmark_info = {\"index\": self.text.index(\"sel.first\"), \"name\": bookmark_name}\n            with open(\"bookmarks.pickle\", \"ab\") as f:\n                pickle.dump(bookmark_info, f)\n        <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"># <\/mark><\/code><\/mark><\/code>\u8df3\u8f6c\u4e66\u7b7e<\/mark><\/code><\/mark><\/code>\n    def go_to_bookmark(self):\n        with open(\"bookmarks.pickle\", \"rb\") as f:\n            bookmarks = []\n            while True:\n                try:\n                    bookmark_info = pickle.load(f)\n                    bookmarks.append(bookmark_info)\n                except EOFError:\n                    break\n        bookmark_names = [bookmark[\"name\"] for bookmark in bookmarks]\n        selected_bookmark_name = simpledialog.askstring(title=\"Go to Bookmark\", prompt=\"Enter bookmark name:\",\n                                                        autocomplete=bookmark_names)\n        if selected_bookmark_name:\n            try:\n                selected_bookmark_info = next(bookmark for bookmark in bookmarks if bookmark[\"name\"] == selected_bookmark_name)\n                self.text.tag_remove(\"sel\", \"1.0\", \"end\")\n                self.text.tag_add(\"sel\", selected_bookmark_info[\"index\"])\n                self.text.mark_set(\"insert\", selected_bookmark_info[\"index\"])\n                self.text.see(\"insert\")\n            except StopIteration:\n                messagebox.showerror(title=\"Error\", message=\"Bookmark not found.\")\n        <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"># <\/mark><\/code><\/mark><\/code>\u5b57\u4f53\u53d8\u5927<\/mark><\/code><\/mark><\/code>\n    def increase_font_size(self):\n        current_size = int(self.text.cget(\"font\").split()[1])\n        new_size = current_size + 2\n        self.text.configure(font=(\"TkDefaultFont\", new_size))\n        <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"># <\/mark><\/code><\/mark><\/code>\u5b57\u4f53\u53d8<\/mark><\/code><\/mark><\/code><\/mark><\/code><\/mark><\/code>\u5c0f<code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\">\n    def decrease_font_size(self):\n        current_size = int(self.text.cget(\"font\").split()[1])\n        new_size = current_size - 2\n        if new_size &gt; 0:\n            self.text.configure(font=(\"TkDefaultFont\", new_size))\n        <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\"># <\/mark><\/code><\/mark><\/code>\u5b57\u4f53<\/mark><\/code><\/mark><\/code><\/mark><\/code><\/mark><\/code>\u53d8\u8272<code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\">\n    def change_font_color(self):\n        color = tk.colorchooser.askcolor()[1]\n        if color:\n            self.text.configure(fg=color)\n\nroot = tk.Tk()\napp = EbookReader(master=root)\napp.mainloop()<\/mark><\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u5c1d\u8bd5\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><noscript><img decoding=\"async\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/05\/image-25.png\" alt class=\"wp-image-854\" width=\"511\" height=\"331\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-25.png 708w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-25-300x194.png 300w\" sizes=\"(max-width: 511px) 100vw, 511px\"><\/noscript><img decoding=\"async\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20511%20331%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-854 lazyload\" width=\"511\" height=\"331\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20511%20331%22%3E%3C%2Fsvg%3E 511w\" sizes=\"(max-width: 511px) 100vw, 511px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-25.png 708w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-25-300x194.png 300w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/05\/image-25.png\"><\/figure>\n\n\n\n<p>\u90a3\u4e48\u81ea\u5df1\u5728pycharm\u91cc\u7ec3\u4e60\u4e00\u4e0b\uff0c\u5e76\u6253\u5305\u751f\u6210exe\u6587\u4ef6\u3002<\/p>\n\n\n\n<!--nextpage-->\n\n\n\n<p>\u6709\u540c\u5b66\u95ee\uff0c\u600e\u4e48\u63d0\u9ad8\u5199\u4ee3\u7801\u7684\u6548\u7387\uff1f\u6280\u5de7\uff1a\u591a\u7ec3\u4e60\uff0ctab\uff0c\u591a\u7528\u63d2\u4ef6\u3002\ud83d\ude1cCodeWhisperer\u548cGitHub Copilot\u4e86\u89e3\u4e00\u4e0b\u3002GitHub Copilot\u514d\u8d39\u8bd5\u7528\u4e00\u4e2a\u6708\uff0cCodeWhisperer\u76ee\u524d\u4ecd\u662f\u514d\u8d39\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5199\u4e00\u4e2a\u7535\u5b50\u4e66\u9605\u8bfb\u5668\uff0c\u7528\u5230\u7684\u4f9d\u7136\u662ftkinter\u6a21\u5757\uff0c\u5b9e\u73b0\u6587\u672c\u7684\u8bfb\u53d6\uff0c\u9000\u51fa\uff0c\u66f4\u6539\u6587\u5b57\u5927\u5c0f\uff0c\u989c\u8272\uff0c\u63d2\u5165\u4e66\u7b7e\uff0c\u8df3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14,10,7],"tags":[91,71,69,90],"class_list":["post-852","post","type-post","status-publish","format-standard","hentry","category-teacher","category-coding","category-software","tag-ebook","tag-python","tag-learning","tag-e_book","entry"],"_links":{"self":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/comments?post=852"}],"version-history":[{"count":0,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/852\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/media?parent=852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/categories?post=852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/tags?post=852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}