我正在使用智能购物车。我已经将RFID阅读器与Raspberry Pi集成在一起,可以将标签卡扫描为产品,并将其添加到购物车中以进行计费。
我想在GUI中添加删除功能按钮。该代码包含
delete()
功能和包含该功能的按钮,但是问题是我可以使按钮起作用,也可以使RFID模块读取标签。
要使这些按钮处于活动状态,我正在呼叫root.mainloop()
,这从技术上来说结束了该程序,并且RfID阅读器在此之后不再扫描标签。如果我确实添加了root.mainloop()
行,则相反。
我是Python的初学者,我们将为您提供帮助。我随附了代码,以供进一步参考。谢谢!
from Tkinter import *
import ttk
import sqlite3
import signal
import time
import sys
from pirc522 import RFID
root = Tk()
root.geometry("800x300")
def delete(self):
selected_item = self.tree.selection()[0] ## get selected item
self.tree.delete(selected_item)
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
'''
def remove_all(self):
x = self.cart.tree.get_children()
print('get children values: ',x,'\n')
if x!= '()':
for child in x:
self.cart.tree.delete(child)
'''
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.button_del = Button(root, text="del",command=delete(self))
self.tree.button_del.pack()
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)
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 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()
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())
self.tree.mainloop()
答案 0 :(得分:0)
我没有要测试的RFid,但是我将创建没有wait_for_rfid
的函数,并使用root.after()
而不是while
。这样,它仍然可以循环运行,但不会阻塞mainloop()
。
def check_rdif():
(error, data) = rdr.request()
if not error:
print("\nDetected: " + format(data, "02x"))
(error, uid) = rdr.anticoll()
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
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())
if run:
# repeate after 20ms (0.2s)
root.after(20, check_rdif)
然后
rdr = RFID()
app = Application(master=root)
print("Starting")
check_rdif()
self.tree.mainloop()
after()
不能直接运行它。它将其发送到mainloop()
,后者检查循环时间,并在20毫秒后运行。
我检查了wait_for_rfid
的源代码,并且还使用了while
循环,因此最终我会在check_rdif
中使用此循环中的代码,但我认为没有必要使用{{1 }}
链接到wait_for_rfid in source code
编辑:未经测试
wait_for_rfid