0

Im trying to develop a GUI to capture images in a new window. In my work i'am trying to pass a directory of the image to be use on future functions, as of now im trying to configure a label to change its text to my image path

import tkinter as tk
from tkinter import *
import cv2
from PIL import Image, ImageTk

from tkinter.filedialog import askopenfilename
import os
from tkinter import messagebox
from pathlib import PosixPath
from numpy import result_type

class CamView():  
    def __init__(self, parent):
        self.parent = parent
        self.window = tk.Toplevel(parent)

        self.lmain2 = tk.Label(self.window)
        self.lmain2.pack()

        self.window.protocol("WM_DELETE_WINDOW", self.close) 
        self.show_frame()
        self.multiple()
        self.btn2 = tk.Button(self.window,text="Close", command = lambda: self.close())
        self.btn2.pack()


    def multiple(self):
        self.takePicture_button = tk.Button(self.window, text="Capture Signature",command = lambda: [Main.captureImage(self.parent),self.close()])
        self.takePicture_button.pack(ipadx=5,ipady=5,pady=5)

    
    def show_frame(self):
        imgtk = ImageTk.PhotoImage(image=self.parent.img)
        self.lmain2.imgtk = imgtk
        self.lmain2.configure(image=imgtk)

    def close(self):
        self.parent.test_frame = None
        self.window.destroy()

root = tk.Tk()
root.bind('<Escape>', lambda e: root.quit())
font_size = 14
window_width = 800
window_height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
frame_dirpath = tk.Frame(root)
frame_dirpath.pack(ipadx=5,ipady=5)
class Main(tk.Frame):
    def __init__(self, parent):


        self.lmain = tk.Label(parent)
        self.lmain.pack()
        self.test_frame = None
        frame = Frame.__init__(self,parent)
        a = Label(text='Verify Signatures:').pack(ipadx=5,ipady=5)
        self.c = tk.Label(text = "check").pack(ipadx=5,ipady=5)
        b = tk.Button(frame, text='CAMERA', command=self.load_window)
        b.pack()
        self.image1_path_entry = tk.Entry(frame_dirpath, font=font_size,width=25).pack(ipadx = 5, ipady = 5)
        width, height = 800, 600
        self.cap = cv2.VideoCapture(0)
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    
        self.do_stuff()



    def do_stuff(self):
        _, frame = self.cap.read()
        frame = cv2.flip(frame, 1)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        self.img = Image.fromarray(cv2image)
        if self.test_frame != None:
            self.test_frame.show_frame()
        self.lmain.after(10, self.do_stuff)


    def captureImage(self):
        image_to_write = self.cap.read()[1]
        filetowrite = "D:/Downloads/c.jpeg"
        print(str(filetowrite))
        cv2.imwrite(str(filetowrite),image_to_write)
        self.c.configure(text = filetowrite)
        return
       


    def load_window(self):
        if self.test_frame == None:
            self.test_frame = CamView(self)


control = Main(root)
root.mainloop()

As on the line self.c = tk.Label(text = "check").pack(ipadx=5,ipady=5) im trying to configure this label to the image path on the function when capturing an image. All i get is object is has no attribute

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • i tried using that method by changing my code to self.c.config(text = str(filetowrite)) still got same error no atrribute 'config' – Joao Roumil Vergara Apr 08 '22 at 14:05
  • The problem isn't in using `self.c`, it's the fact that you assigned None (the result of `.pack()`) to it when you created the widget. – jasonharper Apr 08 '22 at 14:07
  • Can you please elaborate more – Joao Roumil Vergara Apr 08 '22 at 14:10
  • can you please post the entire traceback. [edit] your question to add the information. please review [ask] and [mre]. – Christoph Rackwitz Apr 08 '22 at 14:24
  • Exception in Tkinter callback Traceback (most recent call last): File "C:\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__ return self.func(*args) File "D:\gui\d.py", line 28, in self.takePicture_button = tk.Button(self.window, text="Capture Signature",command = lambda: [Main.captureImage(self.parent),self.close()]) File "D:\gui\d.py", line 93, in captureImage self.c.config(text = str(filetowrite)) AttributeError: 'NoneType' object has no attribute 'config' This is what i get – Joao Roumil Vergara Apr 08 '22 at 14:44
  • `.pack()` returns None. you are following **bad tutorials**. – Christoph Rackwitz Apr 10 '22 at 17:24

1 Answers1

0

Changed this:

self.c = tk.Label(text = "check").pack(ipadx=5,ipady=5)

to:

self.c = tk.Label(frame, text = "check").pack(ipadx=5,ipady=5)

You did same as tk.Button(frame,

toyota Supra
  • 3,181
  • 4
  • 15
  • 19