{"id":3385,"date":"2019-09-07T08:00:17","date_gmt":"2019-09-07T06:00:17","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3385"},"modified":"2019-09-14T21:05:46","modified_gmt":"2019-09-14T19:05:46","slug":"my-personal-notepad-toggle-tkinter-fullscreen","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/my-personal-notepad-toggle-tkinter-fullscreen\/","title":{"rendered":"Python ebooks maker 1"},"content":{"rendered":"<h2>Let&#8217;s make an ebook&#8230; with Python!<\/h2>\n<p>We are going to make an app to create an&#8230; ebook. Well it will be a simple html file, but with a little of efforts it could really become an ebook. The app will be explained in 4 steps (and 4 posts), it will be made with a sort of live coding attitude and there will be many changes on the go. At the end the app will be rough but fully functional and I wish it can give you some ideas to bring the app to a step further making it awesome. Let&#8217;s start.<\/p>\n<h2>Toggle fullscreen? How?<\/h2>\n<p>As we are learning many stuffs about tkinter with the sqlite posts, why don&#8217;t we take a little break and take a look at some other features in a simple project that starts and ends in just one post? In this one we will make a little app with tkinter that will let me check all the content in the files in a folder with this simple layout that you can see in the image and video below:<\/p>\n<figure id=\"attachment_3390\" aria-describedby=\"caption-attachment-3390\" style=\"width: 602px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/finestra.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3390 size-full\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/finestra.jpg\" alt=\"\" width=\"602\" height=\"432\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/finestra.jpg 602w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/finestra-320x230.jpg 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/finestra-321x229.jpg 321w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-3390\" class=\"wp-caption-text\">Our little program with a window that toggles fullscreen with F11<\/figcaption><\/figure>\n<h2>Take a look at the output<\/h2>\n<p>In the following video you will see what we get:<\/p>\n<div style=\"width: 747px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-3385-1\" width=\"747\" height=\"420\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad.mp4?_=1\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad.mp4\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad.mp4<\/a><\/video><\/div>\n<p>This code is meant to let you go a little deeper with:<\/p>\n<ul>\n<li>text widget,\n<ul>\n<li>delete the text, insert the text, make the widget adapt to the window,<\/li>\n<\/ul>\n<\/li>\n<li>create buttons on the go,<\/li>\n<li>toggle tkinter window fullscreen,<\/li>\n<li>and other interesting aspect like for loops, attributes of tkinter etc.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Full code<\/h2>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\r\n# coding: utf-8\r\n# first day of coding: 6.9.19 for my post ob pythonprogramming\r\n\r\nimport tkinter as tk\r\nfrom glob import glob\r\n\r\n\r\nclass Notepad:\r\n    fullscreen = 1 # used by toggle_fullscreen\r\n    def __init__(self, root):\r\n        self.root = root\r\n        self.root.title(\"My personal notepad\")\r\n        self.root.bind(\"&lt;F11&gt;\", lambda x: self.toggle_fullscreen())\r\n        self.root['bg'] = \"coral\"\r\n        self.frame1 = tk.Frame(self.root)\r\n        self.frame1['bg'] = \"coral\"\r\n        self.frame1.pack(side=\"left\")\r\n        #    ---   as many buttons as many files   --- *** a comment by Giovanni G. 6.9.19\r\n        for file in glob(\"*.py\"):\r\n            self.butnew(file, file)\r\n        #    ---   the text that shows the text in the file selected   --- *** commented by G.G. 6.9.19\r\n        self.text = tk.Text(self.root)\r\n        self.text.pack(fill=tk.BOTH, expand=1)\r\n\r\n    def toggle_fullscreen(self):\r\n        \"\"\"   ---   Makes the window in fullscreen or not with F11 key   --- \"\"\"\r\n        if Notepad.fullscreen:\r\n            self.root.attributes(\"-fullscreen\", True)\r\n            Notepad.fullscreen = 0\r\n        else:\r\n            self.root.attributes(\"-fullscreen\", False)\r\n            Notepad.fullscreen = 1\r\n\r\n    def butnew(self, text, file):\r\n        tk.Button(self.frame1, text = text, command= lambda: self.show_code(file)).pack(side=\"top\")\r\n    \r\n    def new_window(self, _class):\r\n        self.new = tk.Toplevel(self.root)\r\n        _class(self.new)\r\n\r\n    def show_code(self, file):\r\n        \"\"\"Button clicked shows the text in the file\"\"\"\r\n        with open(file) as f:\r\n            f = f.read()\r\n        self.text.delete('1.0', tk.END)\r\n        self.text.insert(tk.END, f)\r\n\r\nroot = tk.Tk()  \r\nwin = Notepad(root)\r\nroot.mainloop()\r\n\r\n<\/pre>\n<h2>A version with a list<\/h2>\n<p>In this version we have a list of file instead of buttons that is more handy when there are many files and you need to scroll the list of file names to see them all.<\/p>\n<pre class=\"lang:default decode:true\">import tkinter as tk\r\nfrom glob import glob\r\n\r\nclass Notepad:\r\n\tfullscreen = False\r\n\tdef __init__(self, root):\r\n\t\tself.root = root\r\n\t\tself.root.geometry(\"600x400\")\r\n\t\tself.root.title(\"My notepad\")\r\n\t\tself.root['bg'] = \"coral\"\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.lb = tk.Listbox(self.frame1, width=30)\r\n\t\tself.lb.pack(fill=tk.Y, expand=1)\r\n\t\tfor file in glob(\"*.py\"):\r\n\t\t\tself.lb.insert(tk.END, file)\r\n\t\tself.text = tk.Text(self.root, width=70)\r\n\t\tself.text.pack(fill=tk.BOTH, expand=1)\r\n\t\tself.root.bind(\"&lt;F11&gt;\", lambda x: self.toggle_fullscreen())\r\n\t\tself.lb.bind(\"&lt;Double-Button&gt;\", lambda x: self.show_text())\r\n\r\n\tdef show_text(self):\r\n\t\tnum_item = self.lb.curselection() # the index of item selected\r\n\t\tfname = self.lb.get(num_item) # the file name selected\r\n\t\twith open(fname) as file:\r\n\t\t\tself.text.delete(\"1.0\", tk.END)\r\n\t\t\tfile = file.read()\r\n\t\t\tself.text.insert(tk.END, file)\r\n\r\n\r\n\tdef toggle_fullscreen(self):\r\n\t\tif Notepad.fullscreen == False:\r\n\t\t\tself.root.attributes(\"-fullscreen\", True)\r\n\t\t\tNotepad.fullscreen = True\r\n\t\telse:\r\n\t\t\tself.root.attributes(\"-fullscreen\", False)\r\n\t\t\tNotepad.fullscreen = False\r\n\r\n\r\n\r\n\r\n\r\n\r\nroot = tk.Tk() # creates a window\r\napp = Notepad(root)\r\nroot.mainloop()<\/pre>\n<figure id=\"attachment_3412\" aria-describedby=\"caption-attachment-3412\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3412 size-full\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad.jpg\" alt=\"\" width=\"602\" height=\"432\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad.jpg 602w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad-320x230.jpg 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/mynotepad-321x229.jpg 321w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-3412\" class=\"wp-caption-text\">The second version of the notepad<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" title=\"Python ebook maker - 1\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/HEUmmGc6rd0?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<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":"Let&#8217;s create a sort of notepad with tkinter; discover how to toggle fullscreen with tkinter\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/my-personal-notepad-toggle-tkinter-fullscreen\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3610,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[49],"tags":[534,51],"class_list":["post-3385","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tkinter","tag-notepad","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\/3385","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=3385"}],"version-history":[{"count":11,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3385\/revisions"}],"predecessor-version":[{"id":3612,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3385\/revisions\/3612"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3610"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}