pythontkintertk-toolkitphotoimage

TypeError: 'PhotoImage' object is not callable - Python Tkinter


So I have been trying to build a simple text editor with tkinter but unfortunately when I use the open() function in Python to open a specific file, an error shows up, saying 'TypeError: 'PhotoImage' object is not callable' on line 83. How is this even related to PhotoImage? Could it be possible that this is related to Image.open() ? Here's the code:

from tkinter import *
from tkinter import filedialog, messagebox, ttk
import time
from tkinter.ttk import *
lim = 0
keyItems = ['s', 'x', 'v', 'c', 'o', 'h', 'e', 'n']
def disable_event():
  pass
def creating():
  window.destroy()
  import tkintercontinued
def motion(event):
  textt.config(text='Mouse x: {} Mouse Y: {}'.format(event.x, event.y))
def ssf(event):
  global lim
  if event.keysym == 'Control_L':
      lim = 1
  if lim == 1:
      if event.keysym.lower() == keyItems[0]:
          saveFile()
      elif event.keysym.lower() == keyItems[1] or event.keysym.lower() == keyItems[2] or event.keysym.lower() == keyItems[3]:
          pass
      elif event.keysym.lower() == keyItems[4]:
          openfiles()
      elif event.keysym.lower() == keyItems[5]:
          shortcut()
      elif event.keysym.lower() == keyItems[6]:
          quits()
      elif event.keysym.lower() == keyItems[7]:
          creating()
def install():
  x = 0
  while x < 100:
      bar['value'] += 1
      time.sleep(0.05)
      x+= 1
      p.set(str(x)+ '%')
      window.update_idletasks()
  bar.destroy()
def shortcut():
  e = Tk()
  text = '''This is the help page of a simple text editor (Work-in-progress)
You can use special keys to use commands in the menubar, rather than actually clicking on them!
            -> CTRL + S = Save
            -> CTRL + X = Cut
            -> CTRL + V = Paste
            -> CTRL + C = Copy
            -> CTRL + O = Open
            -> CTRL + H = Help
            -> CTRL + E = Exit
            -> CTRL + N = Create New
            
      '''
  x = Label(e, text=text, font=('Century Gothic', 10))

  x.pack()
def cut():
  pass
def copy():
  pass
def paste():
  pass
def quits():
  e = messagebox.askyesno(title='Exit File', message='Do you want to exit?')
  if e == 1:
      window.grab_release()
      quit()

      return
  else:
      return
def openfiles():
  oo = filedialog.askopenfilename(initialdir="C:/Users/baris/OneDrive/Desktop",
                                        title="Opening file...",
                                        filetypes=[('Text Files', '*.txt'),
                                                   ('Excel Spreadsheet', '*.xlsx *.xls'),
                                                   ('Word Documents', '*.docx *.doc *.docm *.odt *.dot *dotx'),
                                                   ('Python Files', '*.py'),
                                                   ('All Files', '*.*')]
                                        )
  if len(oo) > 1:
      with open(oo) as x:
          text.insert(INSERT, 'Text Imported Successfully! \n')
          text.insert(INSERT, x.read())
          x.close()


window = Tk()
def saveFile():
  file = filedialog.asksaveasfile(initialdir="C:\Games\Python",
                                  defaultextension='.txt',
                                  filetypes=[('Text Files', '*.txt'),
                                                   ('Excel Spreadsheet', '*.xlsx *.xls'),
                                                   ('Word Documents', '*.docx *.doc *.docm *.odt *.dot *dotx'),
                                                   ('Python Files', '*.py'),
                                                   ('All Files', '*.*')])
  if file is None:
      return
  filetext = str(text.get(1.0,END))
  file.write(filetext)
  file.close()
nm = 'C:\\Users\\baris\\Downloads'
notebook = ttk.Notebook(window)
tab1 = Frame(notebook)
tab2 = Frame(notebook)
notebook.add(tab1, text='Tab1')
notebook.add(tab2, text='Tab2')
notebook.pack(expand=True, fill='both')
def Photos(filename, x, y):
  return PhotoImage(file=filename).subsample(x, y)
save = Photos(nm + '\save.png', 25, 25)
open = Photos(nm + '\open.png', 10, 10)
cutf = Photos(nm + '\cut.png', 15, 15)
copyf = Photos(nm + '\copy.png', 5, 5)
pastef = Photos(nm + '\paste.png', 15, 15)
new = Photos(nm + '\\new.png', 25, 25)
exit = Photos(nm + '\exit.png', 20, 20)
help = Photos(nm + '\help.png', 15, 15)
p = StringVar()
pLabel = Label(window, textvariable=p).pack()
text = Text(tab1)
bar = Progressbar(window, orient=HORIZONTAL, length=300)
bar.pack(pady=10)
dwnld = Button(window, text='Download', command=install).pack()
text.pack()
textt = Label(window)
textt.pack()
menubar = Menu(window)
window.config(menu=menubar)
fileMenu = Menu(menubar, tearoff=0, font=('Century Gothic', 10))
editMenu = Menu(menubar, tearoff=0, font=('Century Gothic', 10))
helpMenu = Menu(menubar, tearoff=0, font=('Century Gothic', 10))
menubar.add_cascade(label='File', menu=fileMenu)
menubar.add_cascade(label='Edit', menu=editMenu)
menubar.add_cascade(label='Help', menu=helpMenu)
######
helpMenu.add_command(label='Help', command=shortcut, image=help, compound='left')
editMenu.add_command(label='Cut', command=cut, image=cutf, compound='left')
editMenu.add_command(label='Copy', command=copy, image=copyf, compound='left')
editMenu.add_command(label='Paste', command=paste, image=pastef, compound='left')
fileMenu.add_command(label='Open...', command=openfiles, image=open, compound='left')
fileMenu.add_command(label='Save As...', command=saveFile, image=save, compound='left')
fileMenu.add_command(label='Create New...', command=creating, image=new, compound='left')
fileMenu.add_separator()
fileMenu.add_command(label='Exit', command=quits, image=exit, compound='left')
canvas = Canvas(window,height=50,width=50)
canvas.create_arc(0,0,50,50,fill="red",extent=180,width=1)
canvas.create_arc(0,0,50,50,fill="white",extent=180,start=180,width=1)
canvas.create_oval(19,19,31,31,fill="white",width=1)
canvas.pack()
window.bind('<Key>', ssf)
window.bind('<Motion>', motion)
window.mainloop()


Solution

  • The problem is you assigned the built in function open as a variable to PhotoImage. Now, when you call open, it fetches the value of the open variable because it's assigned a value. This will cause the error. That is why you should never use built-in functions as a variable. You can use open_img or anything that is not a keyword, a built-in function, a function you have defined.

    open = Photos(nm + '\open.png', 10, 10), this is a mistake

    open_img = Photos(nm + '\open.png', 10, 10) This would work .

    Then updated the menu to fileMenu.add_command(label='Open...', command=openfiles, image=open_img, compound='left')