Problems with python code

Asked

Viewed 242 times

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()

2 answers

3


You forgot to put the parameter self in the signature of the method select_image.

  • 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

  • It worked.. that was also a mistake. The other I ended up finding. Thank you very much :D

  • No problem, I was at school so I couldn’t see your other mistake, sorry ;)

3

I ended up finding, the error was that I needed to import the method "filedialog".

from tkinter import filedialog

I just don’t know why you needed it, because "from Tkinter import *" hasn’t been enough.

  • 1

    I explain here: https://answall.com/q/356896/5878

  • Very useful explanation. Thank you Anderson ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.