sharky_machine Offline
Posting Whiz in Training Join Date: Oct 2006
Location: New York City
Posts: 253
Rep Power:

Re: Starting Python
Today, 1:41 pm | Add to sharky_machine's Reputation | Add Infraction | Flag Bad Post | IP | #86


I have been reviewing this code you had submitted in order to understand some elements of Python that are not clear to me.

I am working with TKinter, developing a GUI-based game and so far it is working out very nicely. The GUI itself is pretty much done and the game logic is in my head and will be readily coded in soon. But first I need to make use of a dict (I believe) to map the individual cards for retrieval then display. I understand dictionaries over all (as pertains to key-value, retrieving keys and values, etc, but I am having trouble making the leap from retrieving a keyed GIF (value) then displaying it.

I figure I need to get the value (the GIF) to load into the following line of code, for example, to then render to the GUI as one would normally. I know this may be the incorrect method, possibly, and it does not work so at this point I have ruled it out

d = {'1':'gifToDisplay.gif'}
canvas1.create_image (125,175, gifToDisplay.gif)

I have high-lighted small areas of your code (in italicized magenta ) that I do not quite understand. (** Note: Please be aware, I am just using this code to learn from really, some fundamentals. I do not plan to just insert it into my program) I need to get to get a clearer view of this overall before I move on in a wrong direction.

# 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" ] :

** what does "CDHS" mean\ refer to?

def shuffle_cards(card_list):
"""random shuffle a list of cards"""
# make a copy of the original list
card_list1 = card_list[:]
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


#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 = "D:/Python24/Atest/images/Cards_gif/"

# load a sample card to get the size
photo1 = PhotoImage(file=image_dir+"C2.gif")

# 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)

# 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)


It seems, theoretically, that what I am trying to do is fairly basic-- I believe that I am just missing the link between:
A dict of GIF values --> choosing value (GIF) --> pulling GIF from dict --> display GIF on GUI.

Any\ All input is greatly appreciated,


"You don't carry in your countenance a letter of recommendation."

- Charles Dickens
The Real "sharky_machine":


Last edited by sharky_machine : Today at 1:44 pm. Reason: ++

Just a note:
Please do not clutter the 'Starting Python' sticky with questions, read:

return [ suit + rank for suit in "CDHS" for rank in "A23456789TJQK" ] :
** what does "CDHS" mean\ refer to?

"CDHS" is the suit string: club=C, diamond=D, heart=H spade=S

card_name:image_object dictionary

The 'card_name' for the 8 of heart would be H8, that's the dictionary key.
The dictionary value 'image_object' would be ...
which fleshes out to something like ...

card_list = create_cards()

This creates a new deck of 52 cards.

Hope that helps!

So that is where that went to! I thought I saw that for a moment in the stickies.

You DID see it for a moment in the stickies. :o


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.