0

I have a similar issue described in this post Tkinter entry widget .get doesn't work. I can't extract value from the Entry field. I have tried to fix my code with solution provided, but still getting no response(not even an error) when clicking the "OK" button.

import cv2
import tkinter as tk



cam = cv2.VideoCapture(0)

cv2.namedWindow("Camera")
cam.set(3,1280)
cam.set(4,720)

img_counter = 0



def error_no_frame():
    window=tk.Tk()
    window.geometry("410x200")
    window.title("Error!!!")
    lb=tk.Label(window,text='Please check your camera.\nIt should be installed and connected to your PC', font=("Arial",12))
    lb.place(x=50,y=80)
    window.mainloop()


def rename_pic(x):
    print(x.get())

def entry():
    window=tk.Tk()
    window.geometry("400x150")
    entry_1=tk.StringVar()
    window.title("Let's name your pictures")
    l1=tk.Label(window, text='Case number: ', font=("Arial",12)).grid(row=1,column=1,padx=20,pady=10,ipady=20)
    e1=tk.Entry(window, bd=5, font=("Arial",12),textvariable=entry_1).grid(row=1,column=2)
    b1=tk.Button(window, text="OK",width=20, command=rename_pic(entry_1)).grid(row=2,column=2)

    window.mainloop()


while True:
    ret, frame = cam.read()
    if not ret:
        error_no_frame()
        break
    cv2.imshow("Camera", frame)

    k = cv2.waitKey(1)
    if k%256 == 27:
        print("Escape hit, closing...")
        break

    elif k%256 ==115:
        entry()
    
    elif k%256 == 32:
        img_name = "MIS{}.jpeg".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1

cam.release()

cv2.destroyAllWindows()

Thank you for your help.

Helix05
  • 13
  • 3
  • Your creating two instances of `Tk()` here, which is not recommended at all, instead use `Toplevel()` for child screens and you have to change `e1 = Entry(....).grid(..)` to `e1 = Entry(...)` and then `e1.grid(..)` – Delrius Euphoria Sep 14 '20 at 04:57
  • `command=rename_pic(entry_1)` should be `command=lambda: rename_pic(entry_1)` instead. – acw1668 Sep 14 '20 at 06:30
  • Thank you acw1668. Adding lambda to my code has fixed it. – Helix05 Sep 14 '20 at 13:29

0 Answers0