Search code examples
pythontkinterconfigattributeerrortkinter-canvas

:{ AttributeError: 'int' object has no attribute 'config' in Python


That's my code and I have a proplem with config() method it dosen't work with me when I use it with canvas it give me this error: AttributeError: 'int' object has no attribute 'config' so please help me because I stuck here....

from tkinter import ttk
from tkinter import filedialog

root = Tk()
root.title('3R')
root.geometry('350x300')
root.columnconfigure(0, weight=1)

myCanvas = Canvas(root, width=350, height=300)
myCanvas.pack(fill='both', expand=True)

#File location
Folder_Name =""
def openLocation():
    global Folder_Name
    Folder_Name = filedialog.askdirectory()
    if(len(Folder_Name))>1:
        LocError.config(text=Folder_Name, fg='green')
    else:
        LocError.config(text='Please Choose Folder!!',fg = 'red')
LocError = myCanvas.create_text(175,120, text='You must select a location',fill='red', font=('jost', 10))
#Button for location
SaveLoc = Button(root, text='Choose Path', command = openLocation)
myCanvas.create_window(130,87, anchor='nw', window=SaveLoc)

root.mainloop()```

Solution

  • You have to change your if statement as follows:

    if len(Folder_Name) > 1:
        myCanvas.itemconfig(LocError,text=Folder_Name, fill='green')
    else:
        myCanvas.itemconfig(LocError,text='Please Choose Folder!!',fill='red')
    

    itemconfig takes an id or tag of the canvas item and edits the properties of it. Also there is no fg option for text of canvas, so change that to fill.