Launch Chrome with Python part 3

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.

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.

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.

The code that makes what I described above is the one that you can read below.

#app tp open chrome

import tkinter as tk
import webbrowser

class Check:
    """ create checkbuttons for links """
    def __init__(self, master, site):
        self.var = tk.IntVar()
        self.site = site
        self = tk.Checkbutton(
            master,
            text = self.site,
            variable = self.var,
            command = self.check).pack()

    def check(self):
        v = self.var.get() # 1 checked 0 unchecked
        if v:
            checked.append(self.site)
        else:
            if self.site in checked:
                checked.remove(self.site)

class App:
    def __init__(self, sites):
        """creates the window"""
        self.c = []
        self.sites = sites
        self.master = tk.Tk()
        self.master.geometry("400x400")
        tk.Button(
            self.master,
            text = "Launch",
            command = self.start).pack()
        self.frame = tk.Frame(self.master)
        self.frame.pack()
        for site in sites:
            self.c.append(Check(self.frame, site))
        self.e = tk.Entry(self.master)
        self.e.pack()
        self.e.bind("<Return>", self.addsite)
        self.master.mainloop()

    def addsite(self, site):
        self.sites.append(self.e.get())
        self.c.append(Check(self.frame, self.e.get()))
        with  open("links.txt", "a") as file:
            file.write(self.e.get() + "\n")


    def start(self):
        chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
        w = webbrowser.get(chrome)
        for checked_site in checked:
            w.open(checked_site)

checked = []

# You can put your favourite here
sites = ["www.google.com"]

# Here the new sites are saved when created and loaded at start
with open("links.txt") as file:
    for line in file:
        sites.append(line)

app = App(sites)

The window will have an aspect like this one that you can see in the picture below:

With some changes we had this:

The entry widget that is used to add a new site address is binded with the function addsite that adds ‘on the fly’ a checkbutton with the class (made in this script) Check. Then the address is written (in the append mode ‘a’) in the file links, to memorize it and to have it in the list when you run again the program.

Advertisement