_tkinter.TclError:没有这样的文件或目录

时间:2017-07-21 11:30:06

标签: 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.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

我很确定.py文件和图像文件都在桌面上。我怎么解决这个问题?谢谢!

0 个答案:

没有答案