Skip to content

Instantly share code, notes, and snippets.

@MattWoodhead
Created June 24, 2017 11:09
Show Gist options
  • Save MattWoodhead/ea22bf806f168ea5f7d6f3d1602d88b0 to your computer and use it in GitHub Desktop.
Save MattWoodhead/ea22bf806f168ea5f7d6f3d1602d88b0 to your computer and use it in GitHub Desktop.
Dynamiclaly create buttons from a dictionary
"""
dynamically create buttons with commands in a tkinter gui using a dictionary
"""
import os
import tkinter as tk
# Dictionary of button names and filepaths
PATHS = {"A path": r"C:\Users\Matt\Google Drive\Python\Test\file_1.txt",
"B path": r"C:\Users\Matt\Google Drive\Python\Test\file_2.txt",
"C path": r"C:\Users\Matt\Google Drive\Python\Test\file_3.txt",
}
# Initiate GUI
ROOT = tk.Tk()
# Create buttons that open files using the Dictionary
for key, var in PATHS.items():
button = tk.Button(ROOT, text=key, width=8,
command=lambda x=var: os.startfile(x))
button.pack()
# Start the GUI
ROOT.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment