_tkinter. TclError : couldn’t open “sample.gif”: no such file or directory… here is a solution to the problem.
_tkinter. TclError : couldn’t open “sample.gif”: no such file or directory
Tkinter has a very strange problem.
I can usually display images on tkinter.
However, if I try to load a music file, I can’t display any image on it.
For example,
import tkinter as Tk
class Frame(Tk.Frame):
def __init__(self, master=None):
Tk.Frame.__init__(self, master)
self.f = Tk.Frame(self)
self.f.pack()
self. ARTWORK = ['guthrie.gif']
self.f_artwork = Tk.Frame(self.f)
self.f_artwork.pack()
self.artwork_img = Tk.PhotoImage(file=self. ARTWORK[0])
self.artwork_la = Tk.Label(self.f_artwork, image=self.artwork_img)
self.artwork_la.pack()
if __name__ == '__main__':
f = Frame()
f.pack()
f.mainloop()
In this case I can display the image, however
import tkinter as Tk
import os
from tkinter.filedialog import askdirectory
import pygame
song_list = []
directory = askdirectory()
os.chdir(directory)
for file in os.listdir(directory):
if file.endswith('.mp3'):
realdir = os.path.realpath(file)
song_list.append(file)
pygame.mixer.init()
pygame.mixer.music.load(song_list[0])
pygame.mixer.music.play()
pygame.mixer.init()
pygame.mixer.music.load(song_list[0])
pygame.mixer.music.play()
class Frame(Tk.Frame):
def __init__(self, master=None):
Tk.Frame.__init__(self, master)
self.f = Tk.Frame(self)
self.f.pack()
self. ARTWORK = ['guthrie.gif']
self.f_artwork = Tk.Frame(self.f)
self.f_artwork.pack()
self.artwork_img = Tk.PhotoImage(file=self. ARTWORK[0])
self.artwork_la = Tk.Label(self.f_artwork, image=self.artwork_img)
self.artwork_la.pack()
if __name__ == '__maim__':
f = Frame()
f.pack()
f.mainloop()
For this program, I can’t display images on Tkinter.
The file is not recognized by Tkinter.
The error message is
_tkinter. TclError: Cannot open ‘guthrie.gif’: There is no such file or directory
Can you tell me what the reason is?