Modify the code so that new entries are saved automatically. Code: import tkinter as tk…
- User
Modify the code so that new entries are saved automatically. Code: import tkinter as tk from tkinter import ttk, messagebox import sqlite3
def run_query(query, parameters=()): with sqlite3.connect('media.db') as conn: cursor = conn.cursor() result = cursor.execute(query, parameters) if query.strip().lower().startswith("select"): return result.fetchall() # Ensure to fetch data for SELECT queries conn.commit()
def create_tables(): create_movies_table_sql = ''' CREATE TABLE IF NOT EXISTS movies ( id INTEGER PRIMARY KEY AUTOINCREMENT, movie TEXT, year INTEGER, present BOOLEAN, watched BOOLEAN, notes TEXT ); ''' create_series_table_sql = ''' CREATE TABLE IF NOT EXISTS series ( id INTEGER PRIMARY KEY AUTOINCREMENT, series TEXT, seasons TEXT, year TEXT, present BOOLEAN, seasons_present TEXT, complete BOOLEAN, watched BOOLEAN, seasons_seen TEXT, notes TEXT ); ''' run_query(create_movies_table_sql) run_query(create_series_table_sql)
def load_data(): for row in movies_tree.get_children(): movies_tree.delete(row) for row in series_tree.get_children(): series_tree.delete(row)
for row in run_query('SELECT * FROM movies'): movies_tree.insert('', 'end', values=row[1:]) for row in run_query('SELECT * FROM series'): series_tree.insert('', 'end', values=row[1:])def add_entry(tree, columns): tree.insert('', 'end', values=[''] * len(columns))
def save_edit(tree, item, col, new_value): tree.set(item, column=col, value=new_value)
if tree == movies_tree: columns = ["movie", "year", "present", "watched", "notes"] values = movies_tree.item(item, 'values') run_query(f"UPDATE movies SET {columns[col]} = ? WHERE movie = ? AND year = ?", (new_value, values[0], values[1])) elif tree == series_tree: columns = ["series", "seasons", "year", "present", "seasons_present", "complete", "watched", "seasons_seen", "notes"] values = series_tree.item(item, 'values') run_query(f"UPDATE series SET {columns[col]} = ? WHERE series = ? AND year = ?", (new_value, values[0], values[2]))def edit_cell(event, tree): selected_item = tree.selection() if not selected_item: return col = tree.identify_column(event.x)[1:] if col.isdigit(): col = int(col) - 1
def on_edit_complete(event=None): new_value = entry_var.get() save_edit(tree, selected_item, col, new_value) entry.destroy() x, y, width, height = tree.bbox(selected_item, column=f'#{col + 1}') entry_var = tk.StringVar() entry = tk.Entry(tree, textvariable=entry_var, width=int(width / 7)) entry_var.set(tree.item(selected_item, 'values')[col]) entry.place(x=x, y=y, anchor='nw', height=height, width=width) entry.focus_set() entry.bind("<Return>", on_edit_complete) entry.bind("<FocusOut>", on_edit_complete)def show_movies(): series_frame.forget() movies_frame.pack(fill='both', expand=True)
def show_series(): movies_frame.forget() series_frame.pack(fill='both', expand=True)
- Assistant
- User
instead of the add Add moves and the Add series button show a little + next to the rows. if the user presses it it adds a new row. show the plus only when the user hovers the mouse left of the row.
- Assistant
- User
have a little space between the bar with the buttons and the first collumn and in this space the + button should appear when the user hovers with the mouse over it
- Assistant
- User
tripple the size of the empty bar and also show the + button within the empty bar it should align perefcty with the individual rows the mouse is on and it should be possible for the mouse to click it to add a new row
- Assistant
- User
the + button is not shown in the empty bar between the bar with the Movies and Series butto but it is shown at the left of the gui. Please fix this.
- Assistant
- User
Ok now only show the button when the mouse is in the empty bar between the first cloumn and the Movies button
- Assistant
- User
change the color of the + button to black symbol white backround
- Assistant
- User
add a - button right from the + button also in the empty bar this button should delete the row
- Assistant
- User
increase the size of the empty bar so the - button is visible
- Assistant
- User
The delete button (-) does not work if i press on it the wow does not get deleted
- Assistant
- User
- Assistant