Last night I took the advice of a DaniWeb member and looked at some posted Python code; this code was based around a card-type game and involved creating a GIF deck, shuffling, then displaying it. My original question had to do with using a Python dict for my GIF images.

I downloaded the code and attempted to run it. The problem I keep getting, I'm sure, is not of a Python nature, but a programming problem in general.

Each time I run the code I get this:

C:/Python24/pythonw.exe -u  "C:/Documents and Settings/RockStar/Desktop/Python/drpython-161/LLCool_J.py"
Traceback (most recent call last):
  File "C:/Documents and Settings/RockStar/Desktop/Python/drpython-161/LLCool_J.py", line 61, in ?
    photo1 = PhotoImage(file=image_dir+"C2.gif") 
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 3203, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 3159, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gifC2.gif": no such file or directory

I have these GIF images in the folder with my other Python files; the I tried putting them in a seperate file within this same Python folder. I change the code on every folder file change. I simply keep getting this error.

Has anyone dealt with this particular problem. It seems that it would be easy to solve but I am not finding the right solution.

** Also, does anyone know of a relevant Python error code source (similar to MSDN) that would describe in-depth what the thrown error means and the potential fixes.

Thank-you in advance,
sharky_machine

Recommended Answers

All 5 Replies

C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gifC2.gif looks like your image directory is C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\ and your image file name is Cards_gifC2.gif . You wnat to make this C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\ and then the image name will be C2.gif

C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gifC2.gif looks like your image directory is C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\ and your image file name is Cards_gifC2.gif . You wnat to make this C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\ and then the image name will be C2.gif

Thank-you for your reply.

I followed your advice but I get another error, a result I do not understand; upon Run, it appears that the code appends "C2.gif" to this file location, resulting in: "C2.gifC2.gif" I do not understand why the code below is written this way or what the purpose is when accessing a folder file.

Python source code: area highlighted in blue:

# using Tkinter to display a hand of 5 random card images 
# each time you click the canvas 
# (images are in GIF format for Tkinter to display properly) 
from Tkinter import * 
import random 
root = Tk() 
root.title("Click me!") 
def create_cards(): 
 """ 
 create a list of 52 cards 
 suit: club=C, diamond=D, heart=H spade=S 
 rank: ace=A, 10=T, jack=J, queen=Q, king=K, numbers=2..9 
 ace of spade would be SA, 8 of heart would be H8 and so on ... 
 """ 
 return [ suit + rank for suit in "CDHS" for rank in "A23456789TJQK" ] 
def shuffle_cards(card_list): 
 """random shuffle a list of cards""" 
 # make a copy of the original list 
 card_list1 = card_list[:] 
 random.shuffle(card_list1) 
 return card_list1 
def pick_5cards(card_list): 
 """pick five cards from the shuffled list""" 
 return card_list[:5] 
def create_images(): 
 """create all card images as a card_name:image_object dictionary""" 
 card_list = create_cards() 
 image_dict = {} 
 for card in card_list: 
  # all images have filenames the match the card_list names + extension .gif 
  image_dict[card] = PhotoImage(file=image_dir+card+".gif") 
  #print image_dir+card+".gif"  # test 
 return image_dict 
def next_hand(event): 
 """create the card list, shuffle, pick five cards and display them""" 
 card_list = create_cards() 
 card_list = shuffle_cards(card_list) 
 card_list = pick_5cards(card_list) 
 root.title(card_list)  # test 
 # now display the card images at the proper location on the canvas 
 x = 10 
 y = 10 
 for card in card_list: 
  #print card, x, y  # test 
  canvas1.create_image(x, y, image=image_dict[card], anchor=NW) 
  # calculate each NW corner x, y 
  x += 90 
# change this to the directory your card GIFs are in 
image_dir = "C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\C2.gif" 
# load a sample card to get the size 
photo1 = PhotoImage(file=image_dir+"[B]C2.gif[/B]") 
# make canvas 5 times the width of a card + 100 
width1 = 5 * photo1.width() + 100 
height1 = photo1.height() + 20 
canvas1 = Canvas(width=width1, height=height1) 
canvas1.pack() 
# now load all card images into a dictionary 
image_dict = create_images() 
#print image_dict  # test 
# bind left mouse click on canvas to next_hand display 
canvas1.bind('<Button-1>', next_hand) 
root.mainloop()

Thank-you in advance for any help.

regards,
sharky_machine

Your error is in the line:

image_dir = "C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\C2.gif"

you have to change it to just the directory name:

image_dir = "C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\"

you have to change it to just the directory name:

image_dir = "C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\"

I tried the above code but it returned an error: EOL

I fixed this by appending an additional "\" to this line:

image_dir = "C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\[B]\[/B]"

This worked and the program runs.

It is strange, though; I do not recall seeing this before or ever using the additional "\" in Python in order to access files in a folder.

Thanks again for your help. ;)

regards,
sharky_machine

It depends what character follows the '\', if its 'n' then it becoames an escaped character like '\n' and will behave like a newline. I forgot to add the r in front to avoid this behaviour, like:

image_dir = r"C:\Documents and Settings\RockStar\Desktop\Python\drpython-161\Cards_gif\"

or you can do this:

image_dir = "C:\\Documents and Settings\\RockStar\\Desktop\\Python\\drpython-161\\Cards_gif\\"

That's the way it's done in C/C++ code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.