do_list.py
Nov 25, 2022 3:02:07 GMT -8
Post by Uncle Buddy on Nov 25, 2022 3:02:07 GMT -8
<drive>:\treebard\do_list.py Last Changed 2024-07-03
# do_list
import tkinter as tk
from base import tbard_path
import sqlite3
from widgets import ScrolledDialog, Scrollbar, configall, LabelH3, open_message
from messages import to_do_msg
import dev_tools as dt
from dev_tools import look, seeline
class DoList(tk.Frame):
def __init__(
self, master, formats, vertical_sizer, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.formats = formats
self.vertical_sizer = vertical_sizer
self.highlight_bg = self.formats["highlight_bg"]
self.slidevar = tk.DoubleVar(None, 1.0)
self.make_widgets()
self.make_inputs()
self.draw_do_list()
def make_widgets(self):
self.canvas = tk.Canvas(self)
ScrolledDialog.nested_canvases.append(self.canvas)
self.window = tk.Frame(self.canvas)
self.canvas.create_window(0, 0, anchor="nw", window=self.window)
sbv = Scrollbar(
self, self.formats, command=self.canvas.yview, hideable=False)
self.canvas.config(yscrollcommand=sbv.set, bd=0, highlightthickness=0)
# children of self
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.canvas.grid(column=0, row=0, sticky="news")
sbv.grid(column=1, row=0, sticky="ns")
def make_inputs(self):
head = LabelH3(self.window, self.formats, text="Prioritized To Do List")
self.do_list_frame = tk.Frame(self.window, takefocus=1)
self.do_list_frame.bind("<Delete>", self.destroy_items)
spacer = tk.Frame(self.window, bg="green")
frm2 = tk.Frame(self.window, bd=2, relief='sunken')
frm3 = tk.Frame(frm2)
lab1 = tk.Label(frm3, text="New Item:", anchor="w")
self.item_input = tk.Entry(frm3)
self.priority_input = tk.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 = tk.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, padx=(12,0), pady=(12,0))
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", padx=12)
# children of frm2
frm2.columnconfigure(0, weight=1)
frm3.grid(column=0, row=0, sticky="news", columnspan=2, padx=(0,6))
self.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=(9,0), pady=(6,0))
self.item_input.grid(
column=1, row=0, sticky="ew", padx=(6,0), pady=(6,0))
def draw_do_list(self):
self.update_idletasks()
total = self.vertical_sizer.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)
for child in self.do_list_frame.winfo_children():
child.destroy()
self.update_idletasks()
wraplength = self.vertical_sizer.winfo_reqwidth()
conx = sqlite3.connect(tbard_path)
curx = conx.cursor()
curx.execute(
""" SELECT priority, to_do_id
FROM to_do
""")
lst = curx.fetchall()
self.current_do_list = []
if len(lst) != 0:
lst2 = sorted(lst, key=lambda i: i[0])
do_list = [i[1] for i in lst2]
for idx, text in enumerate(do_list):
lab = tk.Label(
self.do_list_frame, anchor="w", text=text,
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, text))
else:
self.do_list_frame.config(width=wraplength)
configall(self.do_list_frame, self.formats)
curx.close()
conx.close()
self.update_idletasks()
wd = self.window.winfo_reqwidth()
ht = self.vertical_sizer.winfo_reqheight()
scroll_height = self.window.winfo_reqheight()
self.canvas.config(width=wd, height=ht)
self.canvas.config(scrollregion=(0, 0, wd, scroll_height))
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])
conx = sqlite3.connect(tbard_path)
conx.execute('PRAGMA foreign_keys = 1')
curx = conx.cursor()
for tup in list(delete_these):
to_do_item = tup[1]
curx.execute(
''' DELETE FROM to_do
WHERE to_do_id = ?
''',
(to_do_item,))
conx.commit()
self.draw_do_list()
curx.close()
conx.close()
def highlight_items(self, evt):
widg = evt.widget
self.do_list_frame.focus_set()
self.update_idletasks()
if widg.cget("bg") == self.highlight_bg:
widg.config(bg=self.formats["bg"])
else:
widg.config(bg=self.formats["highlight_bg"])
def add(self):
def err_done():
msg1[0].grab_release()
msg1[0].destroy()
self.item_input.focus_set()
self.item_input.delete(0, "end")
new_item = self.item_input.get()
if new_item in [i[1] for i in self.current_do_list]:
msg1 = open_message(self, to_do_msg[0], "Duplicate Do List Item",
"OK", formats=self.formats)
msg1[0].grab_set()
msg1[2].config(command=err_done)
return
priority = self.slidevar.get()
conx = sqlite3.connect(tbard_path)
conx.execute('PRAGMA foreign_keys = 1')
curx = conx.cursor()
curx.execute(
''' INSERT INTO to_do (to_do_id, priority)
VALUES (?, ?)
''',
(new_item, priority))
conx.commit()
self.item_input.delete(0, "end")
self.slidevar.set(1.0)
self.draw_do_list()
curx.close()
conx.close()