I have been working on a somewhat small Python GUI for a few weeks now primarily focusing on the GUI and not giving much in-depth thought to the logic of what is to be a simple card game. I assumed the logic to be in my head and in general it is but I am running into some problems I am having trouble solving.

Before I post some various code examples, I will tell you a bit of the trouble I am having: I have tried using both\ either dictionaries and lists to store card data; I see strengths and weaknesses in each for this program. I am not sure which would be the best at this point as I go back and forth trying different things and neither prove to be the winning answer. The trouble I believe lies in variable scope, specifically around the if\ elifs; there are many as there are 52 cards.

I have been working hard on this and I have tried many things but it seems when I think I solve one thing I have indeed tied myself up logically in another area locking myself into a corner it feels like. I think sometimes: "If I could just break out of the scope and access the nested variable from within." Yes, there is a way, I assume, but either I cannot recall it or do not know of it.

The game will be a basic "21" game. I have researched code and tried other's work but I felt initially I could make something better and do it all on my own. I need some personal assistance from someone knowledgable with this matter as I feel like I am going around and around.

I have learned some much though while doing this code and that is what really counts.

The actual problem reads like this: I wish to print 5 cards to the GUI screen. The Tkinter GUI work I need no help with--it is the logic at this point. I am missing something. I can print the first 5 cards to the screen easy enough but the code is hard-coded as it were and this is of no help-- I need it pseudo-random from a list or dictionary. OR, I run a random list or GUI which works fine but I am getting error messages such as:

C:/Python24/pythonw.exe -u  "C:/Documents and Settings/RockStar/Desktop/Python/drpython-161/phillies.py"
[50, 28, 26, 14, 38, 10, 18, 31, 37, 19, 12, 27, 46, 41, 30, 17, 23, 51, 43, 45, 15, 35, 24, 25, 52, 39, 33, 2, 6, 13, 20, 11, 16, 8, 4, 40, 34, 29, 22, 42, 1, 9, 49, 47, 36, 7, 5, 3, 48, 32, 44, 21]
50
50
x-x-x-x-x-x
50
pyimage1
Traceback (most recent call last):
  File "C:/Documents and Settings/RockStar/Desktop/Python/drpython-161/phillies.py", line 94, in ?
    photo1  = PhotoImage(file=image1)
  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 "50": no such file or directory

This is the primary error I am contending with. I am so close to getting this to run beautifully I think. I have been doing hours of testing and editing, but keep coming back with the same or similar problems. Even when it runs it is just 5, set card defs printed to screen.

Any help, thoughts, directions, or hints will be greatly appreciated.

sharky_machine

GUI runs and renders but only from predefined

from Tkinter import *
import random 
import time 
root = Tk()
#root.wm_iconbitmap('vvvv.ico') 
 
root.title("[21]")
 
#HIT button output ?
def show_image2():
 canvas1.create_image(50, 320, image=photo2)
 
 
# make a list into random order
cards = range(52)
random.shuffle(cards)  # order is random now
pod =  cards [1]       # this takes card, puts in var
print pod      # test print pod
 
 
 
#==================== = = IF for keying int to card GIFs = = # 
 
if pod == 0:           
 print "loaded"
 var = "ewokJ.GIF"      
elif pod == 1:
 print "loaded"
 var = "droidsJ.GIF"
elif pod == 2:
 print "loaded"
 
 
 
cards.remove(pod) ##--- remove card from list
print cards
 
##----- BUILD  checkpoint -----##
#Prep and create card images here
image0  = "base_sith3.GIF"     
photo0  = PhotoImage(file=image0)
image1  = "impbotJ.GIF"
photo1  = PhotoImage(file=image1)
image2  = "trooperJ.GIF"
photo2  = PhotoImage(file=image2)
image3  = "droidsJ.GIF"
photo3  = PhotoImage(file=image3)
image4  = "ewokJ.GIF"
photo4  = PhotoImage(file=image4)
image5  = "chewieQ.GIF"     
photo5  = PhotoImage(file=image5)
image6  = "grievousQ.GIF"
photo6  = PhotoImage(file=image6)
image7  = "fettQ.GIF"
photo7  = PhotoImage(file=image7)
image8  = "hanQ.GIF"
photo8  = PhotoImage(file=image8)
image9  = "darthK.GIF"     
photo9  = PhotoImage(file=image9)
image10 = "empK.GIF"
photo10 = PhotoImage(file=image10)
image11 = "leiaK.GIF"
photo11 = PhotoImage(file=image11)
image12 = "lukeK.GIF"
photo12 = PhotoImage(file=image12)
image13 = "darthA.GIF"
photo13 = PhotoImage(file=image13)
image14 = "empA.GIF"  
photo14 = PhotoImage(file=image14)
image15 = "leiaA.GIF" 
photo15 = PhotoImage(file=image15)
image16 = "lukeA.GIF"  
photo16 = PhotoImage(file=image16)
image17 = "H2.GIF"     
photo17 = PhotoImage(file=image17)
image18 = "H3.GIF"
photo18 = PhotoImage(file=image18)
image19 = "H4.GIF"
photo19 = PhotoImage(file=image19)
image20 = "H5.GIF"
photo20 = PhotoImage(file=image20)
image21 = "H6.GIF"
photo21 = PhotoImage(file=image21)
image22 = "H7.GIF"     
photo22 = PhotoImage(file=image22)
image23 = "H8.GIF"
photo23 = PhotoImage(file=image23)
image24 = "H9.GIF"
photo24 = PhotoImage(file=image24)
image25 = "HT.GIF"
photo25 = PhotoImage(file=image25)
image26 = "C2.GIF"     
photo26 = PhotoImage(file=image26)
image27 = "C3.GIF"
photo27 = PhotoImage(file=image27)
image28 = "C4.GIF"
photo28 = PhotoImage(file=image28)
image29 = "C5.GIF"
photo29 = PhotoImage(file=image29)
image30 = "C6.GIF"
photo30 = PhotoImage(file=image30)
image31 = "C7.GIF"     
photo31 = PhotoImage(file=image31)
image32 = "C8.GIF"
photo32 = PhotoImage(file=image32)
image33 = "C9.GIF"
photo33 = PhotoImage(file=image33)
image34 = "CT.GIF"
photo34 = PhotoImage(file=image34)
image35 = "S2.GIF"     
photo35 = PhotoImage(file=image35)
image36 = "S3.GIF"
photo36 = PhotoImage(file=image36)
image37 = "S4.GIF"
photo37 = PhotoImage(file=image37)
image38 = "S5.GIF"
photo38 = PhotoImage(file=image38)
image39 = "S6.GIF"
photo39 = PhotoImage(file=image39)
image40 = "S7.GIF"     
photo40 = PhotoImage(file=image40)
image41 = "S8.GIF"
photo41 = PhotoImage(file=image41)
image42 = "S9.GIF"
photo42 = PhotoImage(file=image42)
image43 = "ST.GIF"
photo43 = PhotoImage(file=image43)
image44 = "D2.GIF"     
photo44 = PhotoImage(file=image44)
image45 = "D3.GIF"
photo45 = PhotoImage(file=image45)
image46 = "D4.GIF"
photo46 = PhotoImage(file=image46)
image47 = "D5.GIF"
photo47 = PhotoImage(file=image47)
image48 = "D6.GIF"
photo48 = PhotoImage(file=image48)
image49 = "D7.GIF"     
photo49 = PhotoImage(file=image49)
image50 = "D8.GIF"
photo50 = PhotoImage(file=image50)
image51 = "D9.GIF"
photo51 = PhotoImage(file=image51)
image52 = "DT.GIF"
photo52 = PhotoImage(file=image52)
##----BUILD checkpoint ----##
 
 
#Canvas created here \/
# make canvas the size of image1/photo1
width1 = photo0.width()
height1 = photo0.height()
canvas1 = Canvas(width=width1, height=height1)
canvas1.pack()
#display photo1, x, y is center
x = (width1)/2.0
y = (height1)/2.0
canvas1.create_image(x, y, image=photo0)
 
###------Paint cards to screen
time.sleep(3)
root.update()
canvas1.create_image (50,325, image=photo1 )
time.sleep(1)
root.update()
canvas1.create_image (50,175, image=photo2 )
time.sleep(2)
root.update()
canvas1.create_image (125,325, image=photo3 )
time.sleep(2)
root.update()
canvas1.create_image (125,175, image=photo4)
 
#---TEST LOAD from int RNG =========== WORKS
root.update()
canvas1.create_image (200,325, image=photo5)
root.update()
canvas1.create_image (200,325, image=photo6)
root.update()
canvas1.create_image (200,325, image=photo7)
 
#---TEST LOAD from int RNG
 
 
image100 = PhotoImage(file='grey1.gif') # PIC for button---note 100!
# create a button to display card(s)
btn1 = Button(root, text="HIT", image=image100,command=show_image2)# PIC for button---note 100!
btn1. pack()
 
 
root.mainloop()

Will not run

canvas1.pack()
# display photo1, x, y is center
x = (width1)/2.0
y = (height1)/2.0
canvas1.create_image(x, y, image=photo0)
print photo0
## ----- /\ FRAME BUILD /\ ----- ##
 
# setting "photo" to load at bottom for GUI display
image1  = var1   
photo1  = PhotoImage(file=image1)
print var1 
image2  = var2 
photo2  = PhotoImage(file=image2)
image3  = var3
photo3  = PhotoImage(file=image3)
image4  = var4
photo4  = PhotoImage(file=image4)
image5  = var5    
photo5  = PhotoImage(file=image5)
#============ /\  TEST first 5  /\
 
 
image6  = var6
photo6  = PhotoImage(file=image6)
image7  = var7
photo7  = PhotoImage(file=image7)
image8  = var8
photo8  = PhotoImage(file=image8)
image9  = var9     
photo9  = PhotoImage(file=image9)
image10 = var10
photo10 = PhotoImage(file=image10)
image11 = var11
photo11 = PhotoImage(file=image11)
image12 = var12
photo12 = PhotoImage(file=image12)
image13 = var13
photo13 = PhotoImage(file=image13)
image14 = var14
photo14 = PhotoImage(file=image14)
image15 = var15
photo15 = PhotoImage(file=image15)
image16 = var16
photo16 = PhotoImage(file=image16)
image17 = var17   
photo17 = PhotoImage(file=image17)
image18 = var18
photo18 = PhotoImage(file=image18)
image19 = var19
photo19 = PhotoImage(file=image19)
image20 = var20
photo20 = PhotoImage(file=image20)
image21 = var21
photo21 = PhotoImage(file=image21)
image22 = var22     
photo22 = PhotoImage(file=image22)
image23 = var23
photo23 = PhotoImage(file=image23)
image24 = var24
photo24 = PhotoImage(file=image24)
image25 = var25
photo25 = PhotoImage(file=image25)
image26 = var26     
photo26 = PhotoImage(file=image26)
image27 = var27
photo27 = PhotoImage(file=image27)
image28 = var28
photo28 = PhotoImage(file=image28)
image29 = var29
photo29 = PhotoImage(file=image29)
image30 = var30
photo30 = PhotoImage(file=image30)
image31 = var31     
photo31 = PhotoImage(file=image31)
image32 = var32
photo32 = PhotoImage(file=image32)
image33 = var33
photo33 = PhotoImage(file=image33)
image34 = var34
photo34 = PhotoImage(file=image34)
image35 = var35     
photo35 = PhotoImage(file=image35)
image36 = var36
photo36 = PhotoImage(file=image36)
image37 = var37
photo37 = PhotoImage(file=image37)
image38 = var38
photo38 = PhotoImage(file=image38)
image39 = var39
photo39 = PhotoImage(file=image39)
image40 = var40    
photo40 = PhotoImage(file=image40)
image41 = var41
photo41 = PhotoImage(file=image41)
image42 = var42
photo42 = PhotoImage(file=image42)
image43 = var43
photo43 = PhotoImage(file=image43)
image44 = var44     
photo44 = PhotoImage(file=image44)
image45 = var45
photo45 = PhotoImage(file=image45)
image46 = var46
photo46 = PhotoImage(file=image46)
image47 = var47
photo47 = PhotoImage(file=image47)
image48 = var48
photo48 = PhotoImage(file=image48)
image49 = var49     
photo49 = PhotoImage(file=image49)
image50 = var50
photo50 = PhotoImage(file=image50)
image51 = var51
photo51 = PhotoImage(file=image51)
image52 = var52
photo52 = PhotoImage(file=image52)
 
root.mainloop()

Recommended Answers

All 3 Replies

Looks like yo want to open a file called "50" even though the list is all integers.

commented: Ene Uran is always of Great Help. He\ She should Moderate at DaniWeb! +1

Thank-you so much for your help and pointing out this simple solution to me, a glaringly obvious mistake I could not see. Sometimes it takes another person to look at something you've been staring at for hours\ days to see what you are missing.

Thanks again!

sharky_machine

These are just general suggestions, but I hope they will be helpful rather than leading you down a wrong path.

(1) Creating a card class will solve a lot of problems for you. You can do something like this:

class Card(object):
  
  images = {0:"ace_spades.bmp", 1:"two_spades.bmp"} # etc.
  strings = {0:"AS", 1: "2S"} # etc.

  def __init__(self, value=None):
    if not value or value < 0 or value > 51:
      self.value = random.randint[0,51]
    else:
      self.value = value

  def __str__(self):
    return Card.strings[self.value]

  def display(self,canvas):
    # display self on the canvas using Card.images[self.value]

the Card.images value is now a class-wide constant, which I think takes care of the problem with variable scope.

(2) You can then create a Card.used_values list to keep track of already-dealt cards in the hand. You could perhaps modify the __init__ to look up the used_values and not deal any of those.

Hope it helps!

Jeff

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.