在Tkinter GUI表中删除一行

时间:2019-07-03 20:05:34

标签: python sqlite tkinter delete-row

我正在使用智能购物车。在这个项目中,我已将RFID阅读器与Raspberry pi连接。 RFID阅读器读取标签并在tkinter制作的GUI中的表上显示值。现在,我想借助一个按钮从GUI表中删除行条目。

我尝试了几种代码。但是,iam现在使用的删除功能(remove_all(self))似乎更相关,但显示属性错误。我附加我的代码和错误。感谢您的帮助

from Tkinter import *
import ttk
import sqlite3

import signal
import time
import sys
from pirc522 import RFID


class Item(object): 
    def __init__(self, unq_id, name, qty, price):
        self.unq_id = unq_id
        self.product_name = name
        self.price = price
        self.qty = qty



class Cart(object):
    def __init__(self, master=None):
        self.content = dict()
        self.tree= ttk.Treeview(root, column=("column1", "column2", "column3","column4"), show='headings')
        self.tree.heading("#1", text="ITEM ID")
        self.tree.column("#1", anchor = "center", width=135)
        self.tree.heading("#2", text="ITEM NAME")
        self.tree.column("#2", anchor = "center", width=135)                 
        self.tree.heading("#3", text="QUANTITY")
        self.tree.column("#3", anchor = "center", width=135)                 
        self.tree.heading("#4", text="PRICE(Rs)")
        self.tree.column("#4", anchor = "center", width=135) 
        self.tree.pack()



def update(self, unq_id,product_name,price):

    if unq_id not in self.content:
        item = Item(unq_id,product_name,1,price)  
        treeRow=(item.unq_id,item.product_name,item.qty,item.price)#tuple      
        self.tree.insert("", END, values=treeRow)
    else:
        #Already exists
        item = self.content.get(unq_id)
        for index in self.tree.get_children():
            if unq_id == self.tree.item(index)['values'][0]:
                x = index
        item.qty=item.qty+1
        treeRow=(item.unq_id,item.product_name,item.qty,item.price)
        self.tree.item(x, values = treeRow)

    self.content.update({item.unq_id: item})
    return

def get_total(self):
    return sum([v.price * v.qty for _, v in self.content.iteritems()])

def get_num_items(self):
    return sum([v.qty for _, v in self.content.iteritems()])

def remove_item(self, key):
    self.content.pop(key)

def get_item(self, key):
    return self.content.get(key)


class Application(Frame):

    def __init__(self, master=None):
            self.database = "smartShoppingCart.db"        
            Frame.__init__(self, master)
            self.v = StringVar()

            self.cart = Cart(master)
            self.pack()


        conn = sqlite3.connect(self.database)
        cur = conn.cursor()
        cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, Name TEXT, Qty INT, Price REAL, rfidTag INT)")
        #below test case ...will be removed later
        #cur.execute("INSERT INTO profile(id,Name,Qty,Price,rfidTag) VALUES (1, 'Banana', 1, 2., 230)")
        #cur.execute("INSERT INTO profile(id,Name,Qty,Price,rfidTag) VALUES (2, 'Eggs', 2, 5., 131)")
        #cur.execute("INSERT INTO profile(id,Name,Qty,Price,rfidTag) VALUES (3, 'Donut', 3, 1., 128)")
        #above test case ...will be removed later
        conn.commit()
        conn.close()
        Label(root, anchor=W, fg="green", justify=RIGHT, font=("Helvetica", 16), text="Total").pack(side = LEFT)
        btn = Button(root, anchor=W, justify=CENTER, font=("Helvetica", 12), text="Delete", command =remove_all(self) )
        btn.pack()
        Label(root, anchor=W, fg="red", justify=RIGHT, font=("Helvetica", 16), textvariable=self.v).pack(side = RIGHT)
        self.v.set("0.0")



    def insertItemToCart(self, rfidTag):
        #fetch data from database
        string ="SELECT * FROM profile WHERE rfidTag = "+str(rfidTag)
        conn = sqlite3.connect(self.database)
        cur = conn.cursor()
        cur.execute(string)
        rows = cur.fetchall()
        row = rows[0]
        conn.close()
        #update cart
        unq_id=row[4]
        name=row[1]
        price=row[3] 

        self.cart.update(unq_id, name, price)

def remove_all(self):
    x = self.get_children()
    print('get children values: ',x,'\n')
    if x!= '()':
        for child in x:
            self.delete(child)


def end_read(signal,frame):
    global run
    print("\nCtrl+C captured, ending read.")
    run = False
    rdr.cleanup()
    sys.exit()
signal.signal(signal.SIGINT, end_read)


run = True
rdr = RFID()
root = Tk()
root.geometry("800x300")
app = Application(master=root)
app.master.title('Smart shopping cart')

print("Starting")
while run:
    app.update_idletasks()
    app.update()
    rdr.wait_for_tag()
    (error, data) = rdr.request()                
    if not error:
        print("\nDetected: " + format(data, "02x"))
    (error, uid) = rdr.anticoll()
    if not error:
        print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
        time.sleep(1)
        print(uid[0])   
        app.insertItemToCart(uid[0])
        app.v.set(str(app.cart.get_total()))
    print "You have %i items in your cart for a total of $%.02f" % (app.cart.get_num_items(), app.cart.get_total())
root.destroy()

这是显示在外壳程序中的错误

Warning (from warnings module):
  File "/usr/local/lib/python2.7/dist-packages/pi_rc522-2.2.1-py2.7.egg/pirc522/rfid.py", line 78
RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.

Traceback (most recent call last):
  File "/home/pi/Desktop/guitest.py", line 133, in <module>
    app = Application(master=root)
  File "/home/pi/Desktop/guitest.py", line 89, in __init__
    btn = Button(root, anchor=W, justify=CENTER, font=("Helvetica", 12), text="Delete", command =remove_all(self) )
  File "/home/pi/Desktop/guitest.py", line 113, in remove_all
    x = self.get_children()
AttributeError: Application instance has no attribute 'get_children'

1 个答案:

答案 0 :(得分:0)

您在类treeview中创建了Cart,并在类Cart的初始化程序中创建了Application类的实例。因此,要访问您的treeview类中的Applcation,请致电self.cart.tree

class Application(Frame):

    def __init__(self, master=None):
        self.database = "smartShoppingCart.db"        
        Frame.__init__(self, master)
        ...

    def remove_all(self):
        x = self.cart.tree.get_children() #access tree widget from Cart instance
        print('get children values: ',x,'\n')
        if x!= '()':
            for child in x:
                self.cart.tree.delete(child)

如果您需要访问treeview类之外的Application小部件,请使用app.cart.tree,因为您将类的实例创建为app