944,098 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2762
  • Python RSS
Dec 2nd, 2006
0

Re: Starting Python

Expand Post »
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

--------------------------------------------------------------------------------
Vegaseat:

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


Code:
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.


Code:
# 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[:]
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 = "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)
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()


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,

Regards,
sharky_machine
--------------------------------------------------------------------------------

"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:
http://www.daniweb.com/techtalkforums/post104844-2.html

Q1:
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

Q2:
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 ...
PhotoImage(file=image_dir+card+".gif")
which fleshes out to something like ...
PhotoImage(file="D:/Python24/Atest/images/Cards_gif/H8.gif").

Q3:
card_list = create_cards()

This creates a new deck of 52 cards.


Hope that helps!
Similar Threads
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 4th, 2006
0

Re: Starting Python

So that is where that went to! I thought I saw that for a moment in the stickies.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Dec 4th, 2006
0

Re: Starting Python

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

sharky_machine
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: need a help regarding "setup" in pyton using py2exe
Next Thread in Python Forum Timeline: Tk logo on GUI window:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC