Contact book template for tkinter
Open this design in the browser-based designer, rearrange it, and download the Python. Nothing to install, and you don't need an account to try it.
Open in designer →What this template shows you
- Adding a row to a Treeview with .insert("", "end", values=(...))
- Finding what the user picked with .selection(), and deleting it
- Clearing entries after a successful action so the form is ready for the next one
The Python it generates
This is the actual main.py you get when you download it: standard-library
tkinter, no framework, yours to edit:
import tkinter as tk
from tkinter import ttk
class Application:
def __init__(self, root):
self.root = root
root.title("Contact book")
root.geometry("520x420")
self.label1 = tk.Label(root, text="Name")
self.label1.place(x=20, y=20, width=60, height=26)
self.entry1 = tk.Entry(root)
self.entry1.place(x=90, y=18, width=180, height=28)
self.label2 = tk.Label(root, text="Email")
self.label2.place(x=285, y=20, width=55, height=26)
self.entry2 = tk.Entry(root)
self.entry2.place(x=350, y=18, width=150, height=28)
self.button1 = tk.Button(root, text="Add", command=self.on_button1)
self.button1.place(x=20, y=60, width=90, height=32)
self.button2 = tk.Button(root, text="Delete selected", command=self.on_button2)
self.button2.place(x=120, y=60, width=150, height=32)
self.treeview1 = ttk.Treeview(root, columns=("col1", "col2"), show="headings")
self.treeview1.heading("col1", text="Name")
self.treeview1.heading("col2", text="Email")
self.treeview1.place(x=20, y=105, width=480, height=250)
self.label3 = tk.Label(root, text="")
self.label3.place(x=20, y=368, width=480, height=26)
def load_treeview1(self, rows):
"""Populate the table — pass any iterable of row tuples/lists."""
self.treeview1.delete(*self.treeview1.get_children())
for values in rows:
self.treeview1.insert("", "end", values=values)
def on_button1(self):
name = self.entry1.get().strip()
email = self.entry2.get().strip()
if not name:
self.label3.configure(text="Enter a name first.")
return
self.treeview1.insert("", "end", values=(name, email))
self.entry1.delete(0, "end")
self.entry2.delete(0, "end")
self.label3.configure(text="Added " + name)
def on_button2(self):
picked = self.treeview1.selection()
if not picked:
self.label3.configure(text="Select a row to delete.")
return
for row in picked:
self.treeview1.delete(row)
self.label3.configure(text="Deleted " + str(len(picked)) + " row(s)")
if __name__ == "__main__":
root = tk.Tk()
app = Application(root)
root.mainloop()
Save it and run it the usual way:
python main.py Make it your own. Edit the layout visually, then download the code.
Open the contact book template →Other templates
- Login form: Username + password entries with a working submit handler.
- Calculator: Display plus a 4×4 keypad with working press/clear/equals code.
- To-do list: Entry, listbox, and add/remove buttons, all wired up.
- Temperature converter: The classic beginner project: Celsius in, Fahrenheit out.