0

Excuse the poorly written code, but what I am trying to do is make a blackjack game in tkinter.

The problem I am encountering is that the cards output just fine when I hard code them at the beginning of the program, but whenever I hit, it runs through my hit code below which outputs 3 blank "cards" rather than the images they should be.

code:

  randomNum = 0
  printNum = 0
  suits = []
  imgs = []
  for i in range(0,len(dealer.cards),2):
    cardVal.append(int(dealer.cards[i]))
    suits.append(findSuit(dealer.cards[i+1]))
    #print(cardVal)
    #print(suits)
    if cardVal[randomNum] >= 2 and cardVal[randomNum] <= 10:
      imgs.append(ImageTk.PhotoImage((Image.open("cards/"+str(cardVal[randomNum])+"_of_"+suits[randomNum]+".png")).resize((80,120))))
    elif cardVal[randomNum] == 11:
      imgs.append(ImageTk.PhotoImage((Image.open("cards/ace_of_"+suits[randomNum]+".png")).resize((80,120))))
    elif cardVal[randomNum] == 12:
      imgs.append(ImageTk.PhotoImage((Image.open("cards/jack_of_"+suits[randomNum]+".png")).resize((80,120))))
    elif cardVal[randomNum] == 13:
      imgs.append(ImageTk.PhotoImage((Image.open("cards/king_of_"+suits[randomNum]+".png")).resize((80,120))))
    elif cardVal[randomNum] == 14:
      imgs.append(ImageTk.PhotoImage((Image.open("cards/queen_of_"+suits[randomNum]+".png")).resize((80,120))))
    print(imgs[randomNum])
    Label(root,image = imgs[randomNum]).grid(row = 4,column = printNum)
    printNum+=2
    randomNum+=1

before hit: before hit

after hit: after hit

When I moved the ImageTk.PhotoImage to its own variable outside the for loop for a single image for testing, it worked fine. I'm quite new to tkinter in general, so I'm still learning on how to tackle things like this.

Thanks!

  • 1
    See [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091). While not precisely the same question, the root cause is the same. – Bryan Oakley Nov 10 '22 at 05:58
  • @BryanOakley Thanks! This worked, I just had to make imgs global. – Crainingaming Nov 10 '22 at 06:10

0 Answers0