所以我一直试图用tkinter构建一个简单的文本编辑器,但不幸的是,当我在Python中使用open((函数打开特定文件时,会出现一个错误,在第83行显示'TypeError:'PhotoImage'对象不可调用'。这和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:GamesPython",
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()
问题是将内置函数open
作为变量分配给PhotoImage
。现在,当您调用open
时,它会获取open
变量的值,因为它被分配了一个值。这将导致错误。这就是为什么永远不应该将内置函数用作变量的原因。您可以使用open_img
或任何不是关键字、内置函数或您定义的函数的东西。
open = Photos(nm + 'open.png', 10, 10)
,这是一个错误
open_img = Photos(nm + 'open.png', 10, 10)
这是可行的。
然后将菜单更新为fileMenu.add_command(label='Open...', command=openfiles, image=open_img, compound='left')