问题描述
所以我一直在尝试用 tkinter 构建一个简单的文本编辑器,但不幸的是,当我在 Python 中使用 open() 函数打开特定文件时,出现错误,说“TypeError: 'PhotoImage' object is not callable ' 在第 83 行。这与 PhotoImage 有什么关系? 这可能与 Image.open() 有关吗? 代码如下:
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','*.*')])
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)
new = Photos(nm + '\\new.png',25)
exit = Photos(nm + '\exit.png',20,20)
help = Photos(nm + '\help.png',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,10))
editMenu = Menu(menubar,10))
helpMenu = Menu(menubar,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,50,fill="red",extent=180,width=1)
canvas.create_arc(0,fill="white",start=180,width=1)
canvas.create_oval(19,19,31,width=1)
canvas.pack()
window.bind('<Key>',ssf)
window.bind('<Motion>',motion)
window.mainloop()
解决方法
问题是您将内置函数 open
作为变量分配给 PhotoImage
。现在,当您调用 open
时,它会获取 open
变量的值,因为它已被赋值。这将导致错误。这就是为什么你永远不应该使用内置函数作为变量的原因。您可以使用 open_img
或任何非关键字、内置函数、您定义的函数。
open = Photos(nm + '\open.png',10,10)
,这是一个错误
open_img = Photos(nm + '\open.png',10)
这行得通。
然后将菜单更新为
fileMenu.add_command(label='Open...',command=openfiles,image=open_img,compound='left')