How to create an app to launch sites and local files with python

A site launcher with python easy to make.

I am going back to my pylauncher app to make it easier to modify for your purpose. This app creates a GUI with buttons that opens links on the web or other stuffs, simply writing the text, the address and the png file for the button in an external file called sites.site and saving the png file in a folder called icons.
Its goal make a nice GUI to remind you of sites and stuffs you’re working on and to make it with less code

Here you can find the repository on Github in the folder py_site_launcher (in the same repository there are other tkinter examples).

This program lets you create a GUI with tkinter that automatically creates buttons with images, from an external file with the data written like this

mysite #https://formazione.github.io # site.png
google #https://google.com #google.png

The ‘mysite‘ or ‘google‘ names are the text for the buttons, then there is the link that will be opened when you click the button and at last there is the image that will be on the button.

The code, that is very similar to the launcher app we saw on this blog some days ago, is re-thinked to make the things faster and to write less code for this app that is an utility. Later we will make another version to open other apps made in python or any other file on our computer. Think to it as an app launcher, that helps you to remind of your favourite sites or apps, all made with python.

window tkinter
How looks like the window with buttons

This is the full code. You can find the images on the repository at this link here.

import tkinter as tk
import tkinter.ttk as ttk
import os

root = tk.Tk()
root.title("My launcher")


class Button:

    row = 0
    column = 0

    def __init__(self, text, func, arg, image=""):
        if image != "":
            image = tk.PhotoImage(file=image)
        root.rowconfigure(Button.row, weight=1, minsize=40)
        root.columnconfigure(Button.column, weight=1, minsize=40)
        self.button = ttk.Button(
            root,
            text=text,
            image=image,
            compound=tk.LEFT,
            command=lambda: func(arg))

        self.button.grid(
            row=Button.row,
            column=Button.column,
            sticky="nsew", padx=0)
        self.button.image = image
        if Button.column < 5:
            Button.column += 1
        else:
            Button.column = 0
            Button.row += 1


with open("sites.site") as file:
    file = file.readlines()
for line in file:
    x = line.strip()
    x = [f.strip() for f in x.split("#")]
    title, address, image = x
    Button(title, os.startfile, address, image="icons/" + image)

root.mainloop()

Remember to save a sites.site file with the data (for example):

mysite #https://formazione.github.io # site.png
google #https://google.com #google.png

You can add or substitute the data at your will.

Live coding video for py_site_launcher app

In case of problems with the code, contact me on @pythonprogrammi on twitter.

Some code changes

import tkinter as tk
import tkinter.ttk as ttk
from glob import glob
import os
import subprocess


root = tk.Tk()
root.title("My launcher")

class Button:

    row = 0
    column = 0
    def __init__(self, text, func, arg, image=""):
        if image!= "":
            image = tk.PhotoImage(file=image)
        root.rowconfigure(Button.row, weight=1, minsize=40)
        root.columnconfigure(Button.column, weight=1, minsize=40)
        self.button = ttk.Button(
                root,
                text=text,
                image=image,
                compound=tk.LEFT,
                command=lambda: subprocess.Popen(["C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", arg]))
                # command=lambda: func(f"start  {arg}"))

        self.button.grid(row=Button.row, column=Button.column, sticky="nsew", padx=0)
        self.button.image = image
        if Button.column < 5:
            Button.column += 1
        else:
            Button.column = 0
            Button.row += 1

sites = glob("*.site")
print(sites)
if sites != []:
    with open(sites[0]) as file:
        file = file.readlines()
    for line in file:
        x = line.strip()
        x = [f.strip() for f in x.split("#")]
        title, address, image = x
        Button(title, os.system, address, image="icons/" + image)

root.mainloop()

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.