我遇到了一个程序问题,该程序在两个文件中搜索特定的字符串(在这种情况下,是车辆的车牌)。命令行界面上的基本程序运行正常,但是,我似乎无法使GUI版本正常运行。我怀疑它是因为GUI与标准函数的交互方式不同,但我不知道如何解决这个问题。
import csv, sys, tkinter.messagebox
from tkinter import *
mGui = Tk()
def read(Numberplate):
if len(Numberplate) != 7:
return False
with open("Test Data.csv","r") as data:
for row in data:
if Numberplate in row:
global details
details = (row)
return True
else:
return False
data.close()
def read2(Numberplate):
with open("Data.csv","r") as data2:
for row in data2:
if Numberplate in row:
global speed
speed = str("The vehicle\'s speed was " + row[8:] + "mph.")
return True
data2.close()
def callback():
Numberplate = grab.get()
if read(Numberplate) == False:
tkinter.messagebox.showinfo("Results","This numberplate has not been recorded.")
quit()
if read(Numberplate) == True and read2(Numberplate) == True:
tkinter.messagebox.showinfo("Results",(speed + details))
grab = StringVar()
mGui.configure(bg="blue")
mGui.title("Database Searcher")
mlabel = Label(text="Please enter the numberplate in the box below:",bg="purple").grid(row=0,column=3,columnspan=2)
mbutton = Button(mGui,text="Search",command=callback,bg="green").grid(row=5,column=3,columnspan=2)
mbox = Entry(mGui,textvariable=grab).grid(row=3,column=3,columnspan=3)
mGui.mainloop()
这个程序的想法是搜索两个文件,看看它们之间是否有相互的铭牌。如果是这样,他们的速度和驾驶员的详细信息将被提取并打印到消息框中。目前,即使条目正确,所有条目似乎都会返回相同的错误消息。
感谢您阅读,我将不胜感激任何帮助。