I'm new to tkinter so i used a template tkinter code to have multiple page windows each page is in a different class, i thought diving in will make me learn better and it has, and also special thanks to stackoverflow.
the kiosk app is all fullscreen, the start is a greeting with button to switch to frontpage. the front page has a frame for the camera feed and a snapshot button below it. once pressed the image is taken and posted to api to search if face is on database. if true, returns a popup window in the middle of screen with button to return to welcome screen. if false the window is switched to page two and a registration form is presented with a button to end session and return to welcome screen. switching back to welcome screen somehow doesnt reset the app and the camera is always on if i do self.vid.release()
on the snapshot()
function and return to welcome screen to start again the camera frame is just frozen
class FR_GUI(tk.Tk):
def __init__(self):
super().__init__()
#self.title = "Self Registration Kiosk"
self.attributes("-fullscreen", True)
self.vid = cv2.VideoCapture(0)
#get cam width & height
self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)
self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
self.home = WelcomeScreen(self)
self.home.pack(expand=True,fill=tk.BOTH)
self.front = FrontPage(self)
self.second = SecondPage(self)
def welcome_screen(self):
self.front.pack_forget()
self.second.pack_forget()
self.home.pack(expand=True,fill=tk.BOTH)
def show_first(self):
self.home.pack_forget()
self.second.pack_forget()
self.front.pack(expand=True, fill=tk.BOTH)
def show_second(self):
self.front.pack_forget()
self.home.pack_forget()
self.second.pack(expand=True, fill=tk.BOTH)
def create_popup_window(self,person_name):
self.popWin = tk.Toplevel(app)
w=350
h=285
ws=self.popWin.winfo_screenwidth()
hs=self.popWin.winfo_screenheight()
x=(ws/2)-(w/2)
y=(hs/2)-(h/2)
self.popWin.geometry('%dx%d+%d+%d'%(w,h,x,y))
tk.Label(self.popWin, text = "Detected face, "+person_name+" is already registered ").pack()
tk.Button(self.popWin, text = "Return",command = self.restart_win).pack()
def restart_win(self):
self.popWin.destroy()
self.welcome_screen()
class WelcomeScreen (tk.Frame):
def __init__(self, master):
self.master = master
super().__init__(self.master)
self.input = tk.StringVar()
self.mainframe = tk.Frame(self)
self.mainframe.pack(side=tk.TOP,padx=5,pady=80)#expand=True, fill=tk.BOTH)
self.name_label = tk.Label(
self.mainframe,text="Welcome to the self-registration portal",font=("Courier", 18))
self.name_label.pack()
self.ent_btn = tk.Button(
self.mainframe,
text="Enter",
font=("Courier", 15),
command=lambda: self.master.show_first())
self.ent_btn.pack()
class FrontPage(tk.Frame):
def __init__(self, master):
self.master = master
super().__init__(self.master)
self.input = tk.StringVar()
self.engine_ip = '192.168.0.162'
self.username = 'admin'
self.password = 'admin'
self.auth_tok = ''
self.mainframe = tk.Frame(self)
self.mainframe.pack(side=tk.TOP,padx=5,pady=80)#expand=True, fill=tk.BOTH)
self.mid_frame = tk.Frame(self, width = self.master.width, height = self.master.height)
self.mid_frame.pack(side=tk.TOP,padx=5,pady=80)
self.bot_frame = tk.Frame(self)
self.bot_frame.pack(side=tk.TOP, padx=5,pady=20)#,expand = True)
self.name_label = tk.Label(self.mainframe,text="Press Snapshot, picture will be taken in: ",font=("Courier", 13))
self.name_label.pack()
self.label_count = tk.Label(self.mainframe)
self.label_count.pack()
self.cam_label1 = tk.Label(
self.mid_frame,
anchor=tk.CENTER
#text='cam frame'
)
self.cam_label1.pack()
self.ent_btn = tk.Button(
self.bot_frame,
text="Take Snapshot",
font=("Courier", 15),
command=lambda: self.onClick(),
)
self.retrieve_auth_token()
self.ent_btn.pack()
self.cam_show()
def cam_show(self):
ret, self.mid_frame = self.master.vid.read()
#self.mid_frame = cv2.flip(self.mid_frame, 1)
cv2image = cv2.cvtColor(self.mid_frame, cv2.COLOR_BGR2RGBA)
self.img = PIL.Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=self.img)
self.cam_label1.imgtk = imgtk
self.cam_label1.configure(image=imgtk)
self.cam_label1.after(10, self.cam_show)
def countdown(self,count):
self.label_count['text'] = count
if count > 0:
self.after(1000, self.countdown, count-1)
else:
self.snapshot()
def onClick(self):
self.countdown(1)
def retrieve_auth_token(self):
# retrieve auth token function to authenticate API connection
return self.auth_tok
def snapshot(self):
# Get a frame from the video source
ret, self.mid_frame = self.master.vid.read()
ret, out_pic = cv2.imencode('.jpg',self.mid_frame)
str_pic = base64.b64encode(out_pic)
#self.master.vid.release()
prependInfo = 'data:image/jpeg;base64,'
#encodedString = base64.b64encode(img).decode("utf-8")
full_String = str_pic.decode("utf-8")
#print(fullString)
return self.search_multiple(str(full_String))
def search_multiple(self,full_String):
# functions sends image frame to face detection API
requests.post(base_url + urllib.parse.urlencode(parameters),headers=
{'Authorization':self.auth_tok},data=data)
return self.check_registration(r)
def check_registration(self,r):
#function to check API respnse dictionary for detected faces
data_dict = r.json()
person_id_number = data_dict
if person_id_number:
person_name = data_dict[0][0]['name']
print("person was registered with name "+person_name)
self.master.popWin(person_name)
else:
print("person not registered")
self.master.show_second()
class SecondPage(tk.Frame):
def __init__(self, master):
self.master = master
super().__init__(self.master)
#self.clear_widgets()
self.firstName = tk.StringVar()
self.lastName = tk.StringVar()
self.email = tk.StringVar()
self.ph_one = tk.IntVar()
self.mainframe = tk.Frame(self)
self.mainframe.pack(fill=tk.BOTH, pady=80)
self.label = tk.Label(
self.mainframe, text="person not registered. \n please register: "
)
self.label.pack()
self.top_frame = tk.Frame(self)
self.top_frame.pack(side=tk.TOP,padx=5,pady=10)#expand=True, fill=tk.BOTH)
self.center_frame = tk.Frame(self)
self.center_frame.pack(side=tk.TOP,padx=5,pady=10)
self.low_frame = tk.Frame(self)
self.low_frame.pack(side=tk.TOP, padx=5,pady=10)#,expand = True)
fname_label = tk.Label(self.top_frame,text="First Name",font=("Courier", 13))
fname_entry = tk.Entry(self.top_frame, textvariable = self.firstName)
lname_label = tk.Label(self.top_frame,text="Last Name ",font=("Courier", 13))
lname_entry = tk.Entry(self.top_frame, textvariable = self.lastName)
# email_label = tk.Label(self.top_frame,text="Email ",font=("Courier", 13))
# email_entry = tk.Entry(self.top_frame, textvariable = self.email)
phone_label = tk.Label(self.top_frame,text="Contact Number ",font=("Courier", 13))
phone_entry = tk.Entry(self.top_frame, textvariable = self.ph_one)
fname_label.grid(row=0, column=0)
fname_entry.grid(row=0, column=1)
lname_label.grid(row=1, column=0)
lname_entry.grid(row=1, column=1)
# email_label.grid(row=2, column=0)
# email_entry.grid(row=2, column=1)
phone_label.grid(row=3, column=0)
phone_entry.grid(row=3, column=1)
tk.Button(self.low_frame,text='Home',command=self.master.welcome_screen)
.grid(row=0,column=0,pady=4)
tk.Button(self.low_frame,text='Submit',command=self.form_submit)
.grid(row=0,column=1,pady=10)
def form_submit(self):
f_name = self.firstName.get()
l_name = self.lastName.get()
submit_label = tk.Label(self.center_frame,text="Thank you "+f_name+" "+l_name+" for your registeration",font=("Courier", 13))
submit_label.grid(row=2,column=0)
app = FR_GUI()
app.bind("<Escape>", exit)
app.mainloop()
the problem i am facing is when i start the program the camera is on before i switch to the frontpage(first page) were its called! and im still on welcome screen! and also when i return to welcome screen from other pages all the text entries remain. i tried to use destroy() but it just brings back an empty window! is there a way to reset the app after each time user returns to welcome screen? i tried to post this question as minimal as i could.