Last active
November 25, 2022 08:16
-
-
Save RamonWill/36ca01de22e113617b9023a56ed9ac2b to your computer and use it in GitHub Desktop.
The code from my video on how to bind to various widgets in Tkinter (with comments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the Link to my youtube video https://www.youtube.com/watch?v=vBzoLCKQi8U | |
import tkinter as tk | |
import webbrowser | |
from tkinter import ttk | |
def open_link(event): | |
"""Opens the URL on a treeview row in a new browser""" | |
selected_row = treeview_urls.selection() | |
try: | |
row_url = treeview_urls.item(selected_row, "values")[1] | |
print(f"Going to {row_url}") | |
webbrowser.open_new_tab(row_url) | |
except IndexError: | |
print("Invalid row selected") | |
def delete_row(event): | |
"""Deletes the selected row of a treeview""" | |
selected_row = treeview_urls.selection() | |
if selected_row: | |
treeview_urls.delete(selected_row[0]) | |
def display_full_name(event): | |
"""Displays the entries in the first and last name boxes""" | |
first_name = ent_first_name.get() | |
last_name = ent_last_name.get() | |
label_full_name["text"] = f"{first_name} {last_name}" | |
def search_website(event): | |
"""Goes to the website""" | |
website_name = search_box.get().lower() | |
url = f"www.{website_name}.com" | |
print(f"Going to {url}") | |
webbrowser.open_new_tab(url) | |
root = tk.Tk() # intialises the Tkinter GUI | |
root.geometry("850x500") # sets the root window dimenstions to 850w, 500h | |
# Label Frames | |
frame_treeview = tk.LabelFrame(root, text="Learning Resources") | |
frame_treeview.place(relheight=0.5, relwidth=0.7) | |
frame_search = tk.LabelFrame(root, text="Search Box") | |
frame_search.place(relx=0.71, relheight=0.5, relwidth=0.25) | |
# Search Widgets | |
search_box = ttk.Entry(frame_search, text="") | |
search_box.pack() | |
btn_site = tk.Button(frame_search, text="Go to Site") | |
btn_site.pack() | |
# Name Display Widgets | |
ent_first_name = ttk.Entry(frame_search, text="") | |
ent_first_name.pack() | |
ent_last_name = ttk.Entry(frame_search, text="") | |
ent_last_name.pack() | |
label_full_name = ttk.Label(frame_search) | |
label_full_name.pack() | |
# Treeview Widget | |
treeview_urls = ttk.Treeview(frame_treeview) | |
treeview_urls.place(relheight=1, relwidth=1) | |
treescrolly = tk.Scrollbar(frame_treeview, orient="vertical", | |
command=treeview_urls.yview) | |
treescrollx = tk.Scrollbar(frame_treeview, orient="horizontal", | |
command=treeview_urls.xview) | |
treeview_urls.configure(xscrollcommand=treescrollx.set, | |
yscrollcommand=treescrolly.set) | |
treescrollx.pack(side="bottom", fill="x") | |
treescrolly.pack(side="right", fill="y") | |
# Grouping for treeview | |
treeview_urls.insert(parent="", index="end", iid="education", text='Education') | |
# Populating the treeview with data | |
rows = (("Codewars", "www.codewars.com"), | |
("PythonSpot", "www.pythonspot.com/beginner/"), | |
("Reddit", "www.reddit.com/r/learnpython")) | |
header = ("Website Name", "URL") | |
treeview_urls["column"] = header | |
for column in treeview_urls["columns"]: | |
treeview_urls.heading(column, text=column) | |
for row in rows: | |
treeview_urls.insert(parent="education", index="end", values=row) | |
# Bindings | |
# <KeyRelease>: execute method when a key is released | |
# <Button-1>: execute method when the left mouse button is clicked once | |
# <Double-1>: execute method when the left mouse button is clicked twice | |
# <Button-3>: execute method when the right mouse button is clicked once. | |
ent_first_name.bind("<KeyRelease>", display_full_name) | |
ent_last_name.bind("<KeyRelease>", display_full_name) | |
btn_site.bind("<Button-1>", search_website) | |
treeview_urls.bind("<Double-1>", open_link) | |
treeview_urls.bind("<Button-3>", delete_row) | |
root.mainloop() # An infinite loop to keep the GUI running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment