Post by Uncle Buddy on Nov 25, 2022 3:02:07 GMT -8
<drive>:\treebard_gps\app\python\do_list.py Last Changed 2022-11-23
# do_list
import tkinter as tk
import sqlite3
from widgets import (
Frame, Label, Entry, LabelH3, Button, Scale, Canvas, Scrollbar, Scale,
FrameHilited6)
from files import get_current_file
from query_strings import select_to_do, insert_to_do, delete_do_list_item
import dev_tools as dt
from dev_tools import looky, seeline
class DoList(Frame):
def __init__(self, master, tree, right_panel, treebard, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.tree = tree.split("/")[-1].rstrip(".tbd").replace("_", " ").title()
self.right_panel = right_panel
self.treebard = treebard
self.highlight_bg = self.formats["highlight_bg"]
self.slidevar = tk.DoubleVar(None, 1.0)
self.make_widgets()
self.make_inputs()
def make_widgets(self):
self.canvas = Canvas(self)
self.window = Frame(self.canvas)
self.canvas.create_window(
0, 0, anchor="nw", window=self.window)
sbv = Scrollbar(
self, command=self.canvas.yview, hideable=False)
self.canvas.config(yscrollcommand=sbv.set)
# children of self
self.canvas.grid(column=0, row=0, sticky="news")
sbv.grid(column=1, row=0, sticky="ns")
def make_inputs(self):
tree = get_current_file()[0]
conn = sqlite3.connect(tree)
conn.execute("PRAGMA foreign_keys = 1")
cur = conn.cursor()
head = LabelH3(self.window, text="Do List ({})".format(self.tree))
self.do_list_frame = Frame(self.window, takefocus=1)
self.do_list_frame.bind("<Delete>", self.destroy_items)
self.draw_do_list()
spacer = Frame(self.window, bg="green")
frm2 = FrameHilited6(self.window)
frm3 = Frame(frm2)
lab1 = Label(frm3, text="New Item:", anchor="w")
self.item_input = Entry(frm3)
priority_input = Scale(
frm2, variable=self.slidevar, length=180, orient="horizontal",
to=1.0, from_=10.0, resolution=0.1, showvalue=1, label="Priority (low > high):")
add_button = Button(frm2, width=6, text="ADD", command=self.add)
# children of self.window:
self.window.columnconfigure(0, weight=1)
self.window.rowconfigure(2, weight=1)
head.grid(column=0, row=0, sticky="w", columnspan=2)
self.do_list_frame.grid(column=0, row=1, sticky="news", columnspan=2, padx=12, pady=12)
spacer.grid(column=0, row=2, sticky="news", columnspan=2)
frm2.grid(column=0, row=3, sticky="news")
# children of frm2
frm2.columnconfigure(1, weight=1)
frm3.grid(column=0, row=0, sticky="news", columnspan=2, padx=(0,6))
priority_input.grid(column=0, row=1, padx=(6,0), pady=(3,6), sticky="ew")
add_button.grid(column=1, row=1, sticky="se", padx=(0,6), pady=(0,6))
# children of frm3
frm3.columnconfigure(1, weight=1)
lab1.grid(column=0, row=0, sticky="w", padx=(6,0), pady=(6,0))
self.item_input.grid(column=1, row=0, sticky="ew", padx=(6,0), pady=(6,0))
self.update_idletasks()
total = self.right_panel.winfo_reqwidth()
diff = self.do_list_frame.winfo_reqwidth()
item_width = total - (total - diff)
for item in self.do_list_frame.winfo_children():
item.config(wraplength=item_width)
wd = self.window.winfo_reqwidth()
ht = self.right_panel.winfo_reqheight() + 3
self.canvas.config(width=wd, height=ht)
self.canvas.config(scrollregion=self.canvas.bbox('all'))
cur.close()
conn.close()
def draw_do_list(self):
self.current_do_list = []
tree = get_current_file()[0]
conn = sqlite3.connect(tree)
cur = conn.cursor()
for child in self.do_list_frame.winfo_children():
child.destroy()
self.update_idletasks()
wraplength = self.right_panel.winfo_reqwidth() - 72
cur.execute(select_to_do)
do_list = cur.fetchall()
if len(do_list) != 0:
for idx, item in enumerate(do_list):
lab = Label(
self.do_list_frame, anchor="w", text=item[1],
wraplength=wraplength, justify="left")
lab.grid(column=0, row=idx, sticky="ew")
lab.bind("<Double-Button-1>", self.highlight_items)
self.current_do_list.append((lab, item[0]))
else:
self.do_list_frame.config(width=wraplength + 6)
cur.close()
conn.close()
def destroy_items(self, evt):
all_items = self.do_list_frame.winfo_children()
delete_these = []
for idx, child in enumerate(all_items):
if child.cget('bg') == self.highlight_bg:
delete_these.append(self.current_do_list[idx])
tree = get_current_file()[0]
conn = sqlite3.connect(tree)
conn.execute("PRAGMA foreign_keys = 1")
cur = conn.cursor()
for tup in list(delete_these):
widg, to_do_id = tup
cur.execute(delete_do_list_item, (to_do_id,))
conn.commit()
cur.close()
conn.close()
self.draw_do_list()
def highlight_items(self, evt):
widg = evt.widget
self.do_list_frame.focus_set()
if widg.cget("bg") == self.highlight_bg:
widg.config(bg=self.treebard.formats["bg"])
else:
widg.config(bg=self.treebard.formats["highlight_bg"])
def add(self):
new_item = self.item_input.get()
priority = self.slidevar.get()
tree = get_current_file()[0]
conn = sqlite3.connect(tree)
conn.execute("PRAGMA foreign_keys = 1")
cur = conn.cursor()
cur.execute(insert_to_do, (new_item, priority))
conn.commit()
cur.close()
conn.close()
self.item_input.delete(0, "end")
self.slidevar.set(1.0)
self.draw_do_list()