4
I am practicing Tkinter and what I want is to create a button where the user chooses an image and then pass this image to perform another function with opencv.
But when executing the code, the following error appears:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\crist\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
TypeError: select_image() takes 0 positional arguments but 1 was given
what I’m doing wrong?
here is the full code:
from tkinter import *
import cv2
class Application():
def __init__(self, master=None):
self.janela = Frame(master)
self.janela.pack()
self.btn = Button(self.janela, text="Selecione uma Imagem", command=self.select_image)
self.btn.pack(side=BOTTOM)
def select_image():
path = filedialog.askopenfilename()
if len(path) > 0:
image = cv2.imread(path)
return(path)
root = Tk()
Application(root)
root.mainloop()
But then there was another error: Exception in Tkinter callback Traceback (Most recent call last): File "C: Users Crist Appdata Local Programs Python Python37 lib Tkinter_init_.py", line 1705, in call
 return self.func(*args)
 File "D:\Software Project\ProjectRobot\ProjectRobot\ProjectRobot.py", line 14, in select_image
 path = filedialog.askopenfilename()
NameError: name 'filedialog' is not defined
– Cristian Benites
It worked.. that was also a mistake. The other I ended up finding. Thank you very much :D
– Cristian Benites
No problem, I was at school so I couldn’t see your other mistake, sorry ;)
– JeanExtreme002