{"id":3559,"date":"2019-09-13T06:50:26","date_gmt":"2019-09-13T04:50:26","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3559"},"modified":"2019-09-14T20:59:31","modified_gmt":"2019-09-14T18:59:31","slug":"python-makes-ebooks-3-with-quick-recap","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-makes-ebooks-3-with-quick-recap\/","title":{"rendered":"Python ebook maker 3"},"content":{"rendered":"<p>I am trying to make an app to create ebooks with Python. This app needs to be simple, but efficient. I am gonna recap what I have done until now, remaking all the code from scratch to the point where the user can read all the text files stored in a folder. The next step will be to be able to save the modifications made to the files in the text editor inside the app iteself.<\/p>\n<p>The code, redone, is this:<\/p>\n<pre class=\"lang:default decode:true \">import tkinter as tk\r\nimport glob\r\n\r\n\r\nclass Ebook:\r\n\tdef __init__(self, root):\r\n\t\t\"\"\"Define window for the app\"\"\"\r\n\t\tself.root = root\r\n\t\tself.root.geometry(\"600x400\")\r\n\t\tself.root[\"bg\"] = \"coral\"\r\n\t\tself.left_menu()\r\n\t\tself.editor()\r\n\r\n\t# Widgets on the left ===============|\r\n\tdef left_menu(self):\r\n\t\t\"\"\"List of files in the folder\"\"\"\r\n\t\tself.frame1 = tk.Frame(self.root)\r\n\t\tself.frame1[\"bg\"] = \"coral\"\r\n\t\tself.frame1.pack(side='left', fill=tk.Y)\r\n\t\tself.lstb = tk.Listbox(self.frame1, width=30)\r\n\t\tself.lstb['bg'] = \"black\"\r\n\t\tself.lstb['fg'] = 'gold'\r\n\t\tself.lstb.pack(fill=tk.Y, expand=1)\r\n\t\tself.insert_files()\r\n\t\tself.lstb.bind(\"&lt;&lt;ListboxSelect&gt;&gt;\", lambda x: self.show_text())\r\n\r\n\tdef editor(self):\r\n\t\tself.text = tk.Text(self.root)\r\n\t\tself.text['bg'] = \"darkgreen\"\r\n\t\tself.text['fg'] = 'white'\r\n\t\tself.text['font'] = \"Arial 24\"\r\n\t\tself.text.pack(fill=tk.Y, expand=1)\r\n\r\n\r\n\tdef insert_files(self):\r\n\t\t\"\"\"Insert file names in the list on the left\"\"\"\r\n\t\tfiles = glob.glob(\"data\\\\*.txt\")\r\n\t\tprint(files)\r\n\t\tfor file in files:\r\n\t\t\tself.lstb.insert(tk.END, file)\r\n\r\n\tdef show_text(self):\r\n\t\t\"\"\"Shows text of selected file\"\"\"\r\n\t\tnoi = self.lstb.curselection()[0]\r\n\t\tfilename = self.lstb.get(noi)\r\n\t\tfn = self.opentext(filename)\r\n\t\tself.text.delete(\"1.0\", tk.END)\r\n\t\tself.text.insert(tk.END, fn)\r\n\r\n\tdef opentext(self, fname):\r\n\t\twith open(fname) as file:\r\n\t\t\tfile = file.read()\r\n\t\treturn file\r\n\r\n\r\n\r\n\r\nroot = tk.Tk()\r\napp = Ebook(root)\r\napp.root.title(\"Ebook maker\")\r\nroot.mainloop()<\/pre>\n<p>I divided the class Ebook (previosly called Notepad) in more methods, so that is easier to find the code for the single widgets. Until now the widgets are just two:<\/p>\n<ul>\n<li>the listbox with the file list on the left<\/li>\n<li>the text widgets that show the text of the selected files.<\/li>\n<\/ul>\n<p>The listbox (lstb) is in the frame called frame1. The text is &#8216;in&#8217; the root.<\/p>\n<p>That is. Very simple. See the fast live code below:<\/p>\n<p><iframe loading=\"lazy\" title=\"Python Ebook Maker 2\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/wRGG_x6xBt4?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2>Before we go on&#8230; let&#8217;s simplify the code<\/h2>\n<p>A little code adjustment to make it more &#8220;simple&#8221; to read.<\/p>\n<div style=\"width: 747px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-3559-1\" width=\"747\" height=\"420\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/simplify_ebooks3.mp4?_=1\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/simplify_ebooks3.mp4\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/simplify_ebooks3.mp4<\/a><\/video><\/div>\n<pre class=\"lang:default decode:true\">import tkinter as tk\r\nimport glob\r\n\r\n\r\nclass Ebook:\r\n\tdef __init__(self, root):\r\n\t\t\"\"\"Define window for the app\"\"\"\r\n\t\tself.root = root\r\n\t\tself.root.geometry(\"600x400\")\r\n\t\tself.root[\"bg\"] = \"coral\"\r\n\t\tself.menu()\r\n\t\tself.editor()\r\n\r\n\t# Widgets on the left ===============|\r\n\tdef menu(self):\r\n\t\t\"\"\"Listbox on the left with file names\"\"\"\r\n\t\tself.frame1 = tk.Frame(self.root)\r\n\t\tself.frame1[\"bg\"] = \"coral\"\r\n\t\tself.frame1.pack(side='left', fill=tk.Y)\r\n\t\tself.lstb = tk.Listbox(self.frame1, width=30)\r\n\t\tself.lstb['bg'] = \"black\"\r\n\t\tself.lstb['fg'] = 'gold'\r\n\t\tself.lstb.pack(fill=tk.Y, expand=1)\r\n\t\t# ========= insert files name ==========\r\n\t\tfiles = glob.glob(\"lezioni\\\\*.txt\")\r\n\t\tprint(files)\r\n\t\tfor file in files:\r\n\t\t\tself.lstb.insert(tk.END, file)\r\n\t\t# ===== show text on select ============\r\n\t\tself.lstb.bind(\"&lt;&lt;ListboxSelect&gt;&gt;\", lambda x: self.show_text_in_editor())\r\n\r\n\tdef show_text_in_editor(self):\r\n\t\t\"\"\"Shows text of selected file in the editor\"\"\"\r\n\t\tindex = self.lstb.curselection()[0]\r\n\t\tfilename = self.lstb.get(index)\r\n\t\twith open(filename) as file:\r\n\t\t\tcontent = file.read()\r\n\t\tself.text.delete(\"1.0\", tk.END)\r\n\t\tself.text.insert(tk.END, content)\r\n\r\n\tdef editor(self):\r\n\t\t\"\"\"The text where you can write\"\"\"\r\n\t\tself.text = tk.Text(self.root)\r\n\t\tself.text['bg'] = \"darkgreen\"\r\n\t\tself.text['fg'] = 'white'\r\n\t\tself.text['font'] = \"Arial 24\"\r\n\t\tself.text.pack(fill=tk.Y, expand=1)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n\troot = tk.Tk()\r\n\tapp = Ebook(root)\r\n\tapp.root.title(\"Lezioni\")\r\n\troot.mainloop()<\/pre>\n<p>I think that now the code is better, because there are just three methods in the Ebook class:<\/p>\n<ul>\n<li><strong>menu<\/strong>: place the listbox on the left, insert the file names and bind the action to show thr text<\/li>\n<li><strong>editor<\/strong>: place the text editor to the right<\/li>\n<li><strong>show_text_in_editor<\/strong>: the action to show the text linked to the menu method<\/li>\n<\/ul>\n<p>I included some methods into the menu method, because they contained code that belonged to the listbox.<\/p>\n<script>\r\nvar title = \"PyBook the Ebook maker\";\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/my-personal-notepad-toggle-tkinter-fullscreen\/\",\"Pybook part 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/from-a-notepad-to-an-ebook-maker\/\",\"PyBook part 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-makes-ebooks-3-with-quick-recap\/\",\"PyBook part 3\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-ebooks-maker-4-save-files\/\",\"PyBook part 4\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-ebook-maker-4-1-finale\/\",\"PyBook part 4.1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-ebook-maker-update-1\/\",\"Updates until v. 1.6\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/py-ebook-v-1-8-make-ebooks-with-python-wip-dark-mode\/\",\"Version 1.8 Dark mode +-\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/tkinter-how-to-hide-and-show-frames\/\",\"Hide n Show Frames\"]\r\n\t\t];\r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n","protected":false},"excerpt":{"rendered":"Make ebooks with Python and tkinter only\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-makes-ebooks-3-with-quick-recap\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3605,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[551,1,49],"tags":[552,52,4,51],"class_list":["post-3559","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ebooks","category-examples","category-tkinter","tag-ebooks","tag-gui","tag-python","tag-tkinter"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=3559"}],"version-history":[{"count":7,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3559\/revisions"}],"predecessor-version":[{"id":3606,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3559\/revisions\/3606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3605"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}