python-3.xtkinter

how do I delete a label in a TK frame created in one function by using another function


I am creating a small till app for our associative bar, just for fun. I am having trouble with making a function to delete a line input by error. I am sure it is to do with scope but my understanding of the subject is lacking. my question is how do I delete a label in a TK frame created in one function by using another function? The customer’s tab is stored in a small sqlite database from which the delete button removes the last row entered. The idea is then to recreate the display of the tab from the remaining data. the function the I am using does not fail but does not seem to delete the previous label as required.

root = Tk()
root.geometry("1024x600+0+0")
#Full Screen Window
#root.attributes('-fullscreen', True)
root.title('Le Postillion')
products = LabelFrame(root)
products.place(x=0, y=0)

lineHolder = LabelFrame(root)
lineHolder.place(x=540, y=160)

bill = LabelFrame(root)
bill.place(x=540, y=0)

def deleteline():
    conn = sqlite3.connect('BarSales.db')
    connection = conn.cursor()

    connection.execute("DELETE FROM ticket WHERE ROWID = (SELECT MAX(ROWID) FROM ticket)")
    conn.commit()
    conn.close()

#to clear the previously created bill display
    root.winfo_children()[2].destroy()

    conn = sqlite3.connect('BarSales.db')
    connection = conn.cursor()
    connection.execute("SELECT * FROM ticket")
    result1 = connection.fetchall()
    lineNumber = 2
    lineHolder = LabelFrame(root)
    lineHolder.place(x=540, y=160)
    for item in result1:
        x = item[0]
        y = item[1]
        z = item[2]
        print(x, " ", y, " ", z)
        bill1 = Label(lineHolder, text=x,fg="black", width=10, height=1, cursor="hand2").grid(row=lineNumber, column=0)
        bill2 = Label(lineHolder, text=y,fg="black", width=10, height=1, cursor="hand2").grid(row=lineNumber, column=1)
        bill3 = Label(lineHolder, text=z,fg="black", width=10, height=1, cursor="hand2").grid(row=lineNumber, column=2)
        lineNumber += 1

    conn.close()

The deleted line is still visible but when a new product is added it is overwritten and the program functions normaally.


Solution

  • if you’re trying to delete it from the SQL db, you should do:

        DELETE FROM ticket ORDER BY id DESC LIMIT 1
    

    this deletes 1 row from the list when the list is ordered from greatest to least with respect to the incrementing ‘id’ field, which in turn translates to the latest added row. if your current output is ordered in another method you can just change that ‘id’ line to whatever you want to sort it by

    if you want to delete the label itself from the GUI, you can use

        [name of your label].destroy()
    

    you may need to use multi threading to have the tk widget update in real-time. here’s a good article on multi threading and what it does: https://www.geeksforgeeks.org/how-to-use-thread-in-tkinter-python/

    i would also recommend not connecting and closing to the database twice in the same function, just for the sake of reducing redundancies. in actuality, you can connect at the beginning of the .py and close at the end after the .mainloop() on your GUI. that connection object can carry you all the way through your file