我的代码有问题
我用python和tkinter开发我的程序
但我有一个问题让我陷入了困境
我正在构建一个员工附件应用程序
员工有一个ID, 因此,在我的应用程序中,员工在我的表单中的textboxt上输入他/她的id
像这样 enter image description here
问题是,每个员工的len(id)在9到10之间
所以我想在len(id)输入为9之后创建一个事件触发启动
ids = StringVar(value="")
ids.trace("w", lambda name, index, mode, ids=ids: textchanged(ids))
inputid = tkinter.Entry(window,font="Helvetica 20 bold",textvariable=ids)
inputid.pack()
inputid.place(relx=0.5,rely=0.187,width=300,height=30,anchor='sw',)
inputid.size()
def textchanged(ids):
id = ids.get()
if len(id) >= 9:
time.sleep(3)
print(id)
compareimage(id)
问题是,在textbox中输入8个字符id后,textbox停止获取输入 在文本框中输入长度为9的id之前,我无法输入
但在我的控制台中,它仍然可以打印不超过9个长度的id
我怎么解决这个问题呢,
有人能帮我吗
我将此代码添加到我的问题中
def compareimage(id):
try:
path = folder + id
ClassNames = []
mylist = os.listdir(path)
for cls in mylist:
curImg = cv2.imread(f'{path}/{cls}')
image.append(curImg)
ClassNames.append(os.path.splitext(cls)[0])
encodeListKnown = finencode(image)
print(encodeListKnown)
print(ClassNames)
print(mylist)
time.sleep(3)
while (True):
ret, frames = cap.read()
frames =cv2.flip(frames,1)
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2RGB)
imgS = cv2.resize(frames, (0, 0), None, 0.25, 0.25)
imgS = cv2.resize(frames, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facecurentframe = face_recognition.face_locations(imgS)
encodecurframe = face_recognition.face_encodings(imgS, facecurentframe)
encodelistface = []
for faceloc in zip(encodecurframe, facecurentframe):
encodeface = faceloc[0]
encodelistface.append(encodeface)
matches = face_recognition.compare_faces(encodeListKnown, encodelistface[0])
facedistance = face_recognition.face_distance(encodeListKnown, encodelistface[0])
print(encodelistface)
print(facedistance)
print(matches)
a = str(matches)
print(bool(matches))
if (bool(matches) == True):
print("True")
now = datetime.now()
stringnow = now.strftime('%y/%m/%d %H:%M:%S.%f')
f = open(foldertxt, "a")
logabsen = id + " , "+ stringnow + "\n"
f.write(logabsen )
playsound(folderaudio)
encodelistface.clear()
encodeListKnown.clear()
image.clear()
ClassNames.clear()
inputid.delete(0,END)
break
else:
print("False")
encodelistface.clear()
encodeListKnown.clear()
image.clear()
ClassNames.clear()
inputid.delete(0,END)
break
except :
print("There is An Exception" , sys.exc_info()[0])
我重构了您的脚本并删除了所有不必要的绒毛。我还删除了您的等待和您没有提供的
compareimage
调用。它很好用。我的猜测是,等待或对compareimage
的调用正在窃取和/或阻止您的Entry
的焦点。如果您将脚本的我的版本添加到wait
中并调用,一次调用一个,您应该能够找出是什么阻止了Entry
注意:没有理由在
place
之前调用pack
,我也不知道size()
应该做什么ids
已经是全球性的了,因此,您为将其插入lambda
而跳过的所有环都是不必要的。如果某物是>= 9
,那么它也只是> 8
每当更新
inputid
时,可以使用after()
来安排超时任务。您需要在条形码扫描仪每次更新inputid
之间选择足够长的超时时间。如果inputid
在计划的超时之前再次更新,则需要取消上一个超时任务并重新计划另一个超时任务如果超时任务被触发,您可以在超时回调中执行人脸识别,但建议使用线程来执行,因为这可能是一项耗时的任务,将阻塞
tkinter
主循环以下是一个例子:
请注意,按钮
Scan ID
用于模拟条形码阅读器相关问题 更多 >
编程相关推荐