Python _tkinter.TclError:没有这样的文件或目录
下面的代码打算将图像“mole.png”用于tkinter按钮Python _tkinter.TclError:没有这样的文件或目录,python,tkinter,Python,Tkinter,下面的代码打算将图像“mole.png”用于tkinter按钮 import tkinter as tk from tkinter import ttk from tkinter import PhotoImage class whackAMole: numMolesAcross = 4 statusBg = "white" def __init__(self): self.window = tk.Tk() self.moleFram
import tkinter as tk
from tkinter import ttk
from tkinter import PhotoImage
class whackAMole:
numMolesAcross = 4
statusBg = "white"
def __init__(self):
self.window = tk.Tk()
self.moleFrame, self.statusFrame = self.createFrames()
self.molePhoto = PhotoImage(file = "mole.png")
self.moleButtons = self.createMoles() #a list of mole buttons
self.hitCounter, self.missCounter, self.startButton \
= self.createStatusWidgets()
def createFrames(self):
moleFrame = tk.Frame(self.window, width = 300, height = 300,
bg = "grey") #ttk doesn't support bg
moleFrame.grid(row = 0, column = 0)
statusFrame = tk.Frame(self.window, width = 100, height = 300,
bg = "red")
statusFrame.grid(row = 0, column = 1)
return moleFrame, statusFrame
def createMoles(self):
moleButtons = []
for row in range(numMolesAcross):
rowOfMoleButtons = []
for col in range(numMolesAcross):
mole = tk.Button(self.moleFrame, image = self.molePhoto)
mole.grid(row = row, column = col)
rowOfMoleButtons.append(mole)
moeButtons.append(rowOfMoleButtons)
program = whackAMole()
program.window.mainloop()
运行此脚本时,出现以下错误:
Traceback (most recent call last):
File "/Users/Me/Desktop/whackAMole.py", line 40, in <module>
program = whackAMole()
File "/Users/Me/Desktop/whackAMole.py", line 13, in __init__
self.molePhoto = PhotoImage(file = "/Users/Me/Desktop/mole.png ")
File "/Users/Me/anaconda/lib/python3.6/tkinter/__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Users/Me/anaconda/lib/python3.6/tkinter/__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "/Users/Me/Desktop/mole.png ": no such file or directory
回溯(最近一次呼叫最后一次):
文件“/Users/Me/Desktop/whackAMole.py”,第40行,在
program=whackAMole()
文件“/Users/Me/Desktop/whackAMole.py”,第13行,在__
self.molePhoto=PhotoImage(文件=“/Users/Me/Desktop/mole.png”)
文件“/Users/Me/anaconda/lib/python3.6/tkinter/_init__.py”,第3539行,在_init中__
图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦)
文件“/Users/Me/anaconda/lib/python3.6/tkinter/_init__.py”,第3495行,在_init中__
self.tk.call(('image','create',imgtype,name,)+选项)
_tkinter.TclError:无法打开“/Users/Me/Desktop/mole.png”:没有这样的文件或目录
我很确定.py文件和图像文件都在桌面上。我怎样才能解决这个问题?谢谢大家! “相当肯定”?你需要确定。Python通常不会报告文件不存在,除非它确实不存在。您需要做的第一件事就是确认
/users/Me/Desktop/mole.png
是否存在。当您在命令提示符下键入ls/Users/Me/Desktop/mole.png
时会发生什么?啊……我的意思是我很确定。该命令给了我一个错误:处理magic函数时出错:[Errno 20]不是目录:“/Users/me/Desktop/mole.png”第232行,位于/Applications/pyzo.app/Contents/MacOS/source/pyzo/pyzokernel/magic.py中