{"id":2844,"date":"2019-08-12T12:35:23","date_gmt":"2019-08-12T10:35:23","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2844"},"modified":"2019-08-12T12:35:23","modified_gmt":"2019-08-12T10:35:23","slug":"launch-chrome-with-python-part-3","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/launch-chrome-with-python-part-3\/","title":{"rendered":"Launch Chrome with Python part 3"},"content":{"rendered":"<p>This time, in this third part of this topic about webbrowser to open Chrome with Python, we will add the chance to add new sites from the GUI and not just adding the sites to the list in the script, like we could do with the previous script.<\/p>\n<p>The new sites will be memorized in an external txt file called links.txt. Save it first also empty. You could also write the list of name of the sites into the txt file.<\/p>\n<p>When you will run the program it will read the external file to show the new list in the window. The next feature that we will add in the next part will be the chance to delete the links that we do not need anymore.<\/p>\n<p>The code that makes what I described above is the one that you can read below.<\/p>\n<pre class=\"lang:default decode:true\">#app tp open chrome\r\n\r\nimport tkinter as tk\r\nimport webbrowser\r\n\r\nclass Check:\r\n    \"\"\" create checkbuttons for links \"\"\"\r\n    def __init__(self, master, site):\r\n        self.var = tk.IntVar()\r\n        self.site = site\r\n        self = tk.Checkbutton(\r\n            master,\r\n            text = self.site,\r\n            variable = self.var,\r\n            command = self.check).pack()\r\n\r\n    def check(self):\r\n        v = self.var.get() # 1 checked 0 unchecked\r\n        if v:\r\n            checked.append(self.site)\r\n        else:\r\n            if self.site in checked:\r\n                checked.remove(self.site)\r\n\r\nclass App:\r\n    def __init__(self, sites):\r\n        \"\"\"creates the window\"\"\"\r\n        self.c = []\r\n        self.sites = sites\r\n        self.master = tk.Tk()\r\n        self.master.geometry(\"400x400\")\r\n        tk.Button(\r\n            self.master,\r\n            text = \"Launch\",\r\n            command = self.start).pack()\r\n        self.frame = tk.Frame(self.master)\r\n        self.frame.pack()\r\n        for site in sites:\r\n            self.c.append(Check(self.frame, site))\r\n        self.e = tk.Entry(self.master)\r\n        self.e.pack()\r\n        self.e.bind(\"&lt;Return&gt;\", self.addsite)\r\n        self.master.mainloop()\r\n\r\n    def addsite(self, site):\r\n        self.sites.append(self.e.get())\r\n        self.c.append(Check(self.frame, self.e.get()))\r\n        with  open(\"links.txt\", \"a\") as file:\r\n            file.write(self.e.get() + \"\\n\")\r\n\r\n\r\n    def start(self):\r\n        chrome = \"C:\/Program Files (x86)\/Google\/Chrome\/Application\/chrome.exe %s\"\r\n        w = webbrowser.get(chrome)\r\n        for checked_site in checked:\r\n            w.open(checked_site)\r\n\r\nchecked = []\r\n\r\n# You can put your favourite here\r\nsites = [\"www.google.com\"]\r\n\r\n# Here the new sites are saved when created and loaded at start\r\nwith open(\"links.txt\") as file:\r\n    for line in file:\r\n        sites.append(line)\r\n\r\napp = App(sites)\r\n<\/pre>\n<p>The window will have an aspect like this one that you can see in the picture below:<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome003-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2845\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome003-1.png\" alt=\"\" width=\"490\" height=\"238\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome003-1.png 490w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome003-1-320x155.png 320w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>With some changes we had this:<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome_3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2846\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome_3.png\" alt=\"\" width=\"618\" height=\"283\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome_3.png 618w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/chrome_3-320x147.png 320w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/a><\/p>\n<p>The entry widget that is used to add a new site address is binded with the function addsite that adds &#8216;on the fly&#8217; a checkbutton with the class (made in this script) Check. Then the address is written (in the append mode &#8216;a&#8217;) in the file links, to memorize it and to have it in the list when you run again the program.<\/p>\n","protected":false},"excerpt":{"rendered":"Open sites on Chrome with python adding new sites to the GUI\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/launch-chrome-with-python-part-3\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2847,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[443],"tags":[501,45],"class_list":["post-2844","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chrome","tag-chroe","tag-webbrowser"],"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\/2844","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=2844"}],"version-history":[{"count":1,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2844\/revisions"}],"predecessor-version":[{"id":2848,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2844\/revisions\/2848"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2847"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}