A graphic user interface with Python and tkinter to launch different applications in an easy way.
I found Python very useful when you need to automate something, to find easy solutions for your work on daily basis. So I thought to this very simple program to launch other applications through python. You can launch text files, docs, excels or other python’s script… whatever. Here is the code for a very basic launcher.
import os import tkinter as tk import tkinter.ttk as ttk root = tk.Tk() root.title("Launcher") root.geometry("400x400") class Button: def __init__(self, text, func, row, col): self.button = ttk.Button( root, text=text, command=func) self.button.grid(row=row, column=col, sticky=tk.NW, pady=0) def open_text(text): os.startfile(text) Button("Open example text", lambda x: Button.open_text("example_for_launch.txt"), 0, 0) Button("Open example text 2", lambda x: Button.open_text("Hello World 2.txt"), 1, 0) root.mainloop()
P.S.: other possible thing to launch
Another thing that I use to do it with the launcher is to launch my favourite sites and even the last post I am doing on word press site (this one), so that I can immediately go into the post to add what I want in just a click, using the same method used to open a text file, giving as argument the address of the post.
What if you want an image in the button?
I thought that the interface could be nicer with some images in the button, so there code is to do it.
import os import tkinter as tk import tkinter.ttk as ttk root = tk.Tk() root.title("Launcher") root.geometry("400x400") class Button: def __init__(self, text, func, row, col, image=""): image2 = tk.PhotoImage(file=image) image3 = image2.subsample(1, 1) self.button = ttk.Button( root, text=text, image=image3, compound = tk.LEFT, command=func) # self.button.pack() self.button.grid(row=row, column=col, sticky=tk.NW, pady=0) self.button.image = image3 def open_text(text): os.startfile(text) print(os.getcwd()) Button("Open example text", lambda x: Button.open_text("example_for_launch.txt"), 0, 0, image="notepad.PNG") Button("Open example text 2", lambda x: Button.open_text("Hello World 2.txt"), 1, 0, image="notepad.PNG") root.mainloop()
and here is the result:
Another example of how we can make a better interface with images on the buttons.
Other posts about buttons in tkinter
Perfect code to expand buttons with simplicity
This is an update with the really perfect code to make the button expand correctly. It was a bit complicated to understand how to make the button expand correctly, but, finally, here is the result.
import os import tkinter as tk import tkinter.ttk as ttk root = tk.Tk() root.title("Launch my Programs") class Button: row = 0 def __init__(self, text, func, image=""): image = tk.PhotoImage(file=image) tk.Grid.rowconfigure(root, Button.row, weight=1) tk.Grid.columnconfigure(root, 0, weight=1) self.button = tk.Button( root, text=text, image=image, compound = tk.LEFT, command=func) # self.button.pack() self.button.grid(sticky="nswe") self.button.image = image Button.row += 1 def open(text): os.startfile(text) data = [ ["Text 1", lambda: Button.open("example.txt"), "notepad.PNG"], ["Text 2", lambda: Button.open("example2.txt"), "notepad.PNG"], ] for d in data: b = Button(*d) root.mainloop()
This is the output, being the window stretched:
This is another example.
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame
Videos
Speech recognition gamePygame's Platform Game
Other Pygame's posts