2

I'm writing a code about an image processor in Python. I want to open a window with some tools for image processing, to open the image I'm using the function filedialog.askopenfile from tkinter. I created a openImage() function that asks the file path to the user, then displays the image with a menubar and some frames but every time that I run the program the filedialog.askopenfile function runs without invoking it, and I don't know hot to fix it. I need help because I'm having this problem since a couple of hours :(

Here is my code:

import tkinter as tk
from tkinter import filedialog
from tkinter.ttk import Label
import cv2
from PIL import Image, ImageTk
import webbrowser

def openImage():
    fileLocation = filedialog.askopenfile(initialdir='/', title='Selecciona Un Archivo',
                                              filetypes=(('png files', '*.png'), ('all files', '*.')))
    global image
    image = cv2.imread(fileLocation)
    cv2.waitKey(0)
    cv2.destroyAllWindows()



class Main(tk.Tk):
    def __init__(self):
        super().__init__()


        self.title('Programa de Manipulación de Imagenes de JMMS')
        self.geometry('600x600')
        self.iconbitmap('tools/triforce.ico')

        menu = tk.Menu(self)
        filemenu = tk.Menu(menu, tearoff=0)

        filemenu.add_command(label="Abrir Imagen", command=openImage())
        filemenu.add_command(label="Guardar Imagen", command=None)
        filemenu.add_separator()
        filemenu.add_command(label="Salir", command=self.quit)
        menu.add_cascade(label="Archivo", menu=filemenu)

        toolsmenu = tk.Menu(menu, tearoff=0)
        toolsmenu.add_command(label="Zoom", command=None)
        menu.add_cascade(label="Herramientas", menu=toolsmenu)

        helpmenu = tk.Menu(menu, tearoff=0)
        helpmenu.add_command(label="Contacto", command=None)
        menu.add_cascade(label="Ayuda", menu=helpmenu)

        self.config(menu=menu)

        toolsFrame = tk.Frame(self, width=50, height=600, background='white')
        toolsFrame.grid(row=1, column=1)

        frame = tk.Frame(self, width=500, height=550, background='gray')
        frame.grid(row=1, column=2)

        frame3 = tk.Frame(self, width=50, height=600, background='white')
        frame3.grid(row=1, column=3)



if __name__ == '__main__':
    main = Main()
    main.mainloop()

  • image = cv2.imread(fileLocation) TypeError: Can't convert object to 'str' for 'filename'. But I can add filename. then I can see menu. But no image on cv2. – toyota Supra Jun 12 '22 at 09:13

1 Answers1

1

In this line:

filemenu.add_command(label="Abrir Imagen", command=openImage())

the parentheses after openImage cause the problem. Remember, putting the parentheses there calls the function immediately, leading to the effect you're seeing. That's not what you want. You want to pass the function as the command so that the code can then call it when it needs to.

So, you should just have command=openImage there.

To be a bit more explicit: the code you have passes the return value of openImage as command, whereas dropping the parenthesis passes the function openImage as command so it can be invoked later.

ndc85430
  • 1,395
  • 3
  • 11
  • 17