Christopher Hatton
Christopher Hatton

Reputation: 63

Why am I gettin 'TypeError: 'Button' object is not callable'?

I'm trying to make a text editor. I am trying to add an option to open a file, but it comes up with an error for line 16. TypeError: 'Button' object is not callable

This is my code:

from tkinter import *
from tkinter import filedialog

root = Tk()

root.title("Microsoft Word")

def open_file():
    file_location = ''
    file_location = filedialog.askopenfilename()
    f = open(file_location,'r')
    x = f.read()
    f.close()
    print(x)

heading = Label(text = 'Microsoft Word', font='Helvetica 40 bold')
heading.grid(row = 0,column = 0)

text = Text()
text.grid(row = 1,column = 0)

open = Button(text = 'Open',command = open_file)
open.grid(row = 2,column = 0)

save = Button(text = 'Save')
save.grid(row = 3,column = 0)

root.mainloop()

Upvotes: 0

Views: 156

Answers (1)

Muhd Mairaj
Muhd Mairaj

Reputation: 677

In your code you define open as a variable to be one of the Button

The problem is that open is a built in function and when you do that, it gets redefined to be a Button object.

Now when you try to open a file with the function open, it gives that error because open has been defined as a Button.

btn = Button(text = 'Open',command = open_file)
btn.grid(row = 2,column = 0)

Upvotes: 2

Related Questions