我正在使用Rasberry pi扫描条形码并从Sqlite数据库获取数据以显示在屏幕上。问题是我的数据库表中有一个图像名称,图像位于pi Pi SD卡上的文件夹中,但是我不知道如何在扫描代码时更改图像。这是我的代码
from Tkinter import *
from PIL import ImageTk,Image
import tkMessageBox
import sqlite3
import os
root = Tk()
root.attributes("-fullscreen", True) # set to fullscreen
# Create the Database
connection = sqlite3.connect('/share/pawsnclaws.KDB')
cursor = connection.cursor()
# Initializes Variables
line = StringVar()
code = StringVar()
name = StringVar()
price = StringVar()
path = StringVar()
img_value = StringVar()
imgs = StringVar()
# Defining Functions
def key(event):
root.focus_set()
#global line
#print event.char
if event.char != '\r':
line.set(line.get() + str(event.char))
print line.get()
#print str(event.char)
else:
#print line.get()
helloCallBack(line.get())
line.set("")
def helloCallBack(msg):
cursor.execute('Select * from inventory where Inventory_EnterCode = ?',(msg,))
rows = cursor.fetchall()
for row in rows:
pid = row[6]
pname = row[2]
pprice = row[7]
pimage = row[9]
code.set(pid)
name.set(pname)
price.set(pprice)
img_value.set(pimage)
#tkMessageBox.showinfo('Hello Python', path.get())
def callback(event):
#root.focus_set()
print "clicked at", event.x, event.y
def end_fullscreen(event):
root.attributes("-fullscreen", False)
# Setting Initial Values
code.set("CHK-CE-101")
name.set("LCD")
price.set("5600")
path.set("/share/images/")
img_value.set("bg.jpg")
# Control Initialization
Label_1 = Label(root,text="Code :",font="Calibri 18 bold",height="0")
Label_2 = Label(root,textvariable=code,font="Calibri 18",height="0",anchor="w")
Label_3 = Label(root,text="Name :",font="Calibri 18 bold",height="0")
Label_4 = Label(root,textvariable=name,font="Calibri 20 ",height="2",anchor='w')
Label_5 = Label(root,text="Price :",font="Calibri 18 bold",height="0")
Label_6 = Label(root,textvariable=price,font="Calibri 50 ",height="0",anchor="w",bg="red",fg="white")
Label_9 = Label(root,text="",font="Calibri 14 bold",height="0")
Label_10 = Label(root,text="",font="Calibri 14 ",height="0")
# Side Image 200 x 200
img = ImageTk.PhotoImage(Image.open(os.path.join(path.get(),img_value.get())))
panel = Label(root, image = img,width="200",height="200")
panel.grid(row=5, column=2, columnspan=6, rowspan=4)
# Header Image 800 x 200
canvas = Canvas(root, width = 800, height = 200)
canvas.grid(row=0, column=0, columnspan=4, rowspan=4)
img_1 = ImageTk.PhotoImage(Image.open("/share/back.jpg"))
canvas.create_image(0, 0,anchor = NW, image=img_1)
root.bind("<Key>", key)
root.bind("<Button-1>", callback)
root.bind("<Escape>", end_fullscreen)
Label_9.grid(row=4,column=0)
Label_10.grid(sticky=W,row=4,column=1)
Label_1.grid(row=5,column=0)
Label_2.grid(sticky=W, row=5,column=1)
Label_3.grid(row=6,column=0)
Label_4.grid(sticky=W, row=6,column=1)
Label_5.grid(row=7,column=0)
Label_6.grid(sticky=W, row=7,column=1)
root.mainloop()
现在,我每次扫描条形码时都必须更改代码以更改图像。我在这里使用Raspberry Pi 3 ...在这方面的任何帮助将不胜感激。预先感谢