简体   繁体   中英

Image not showing on a label in Tkinter

HI I'm writing a program that deals 26 cards to the user and computer. So far, I only have a button that displays the card that is at the top of the users deck. I have a label and I have a folder of card images named with a capital first letter of suit, H,S,C,D, and the card, 2,3,4...,10,J,Q,K,A. so for example, the 5 of hearts is H5.bmp. They are all .bmp files. the program is in the same folder as the card images.

they are all in a folder called cards. I am running python 2.5 and Tkinter as GUI builder.

from random import choice
from Tkinter import *
suits=['H','S','C','D']
cards=['2','3','4','5','6','7','8','9','10','J','Q','K','A']
user=[]
comp=[]
used=[]
userturn=True

def deal():
    global user,comp,used
    numcards=1
    while numcards<=26:
        current=(choice(suits),choice(cards))
        while current in used:
            current=(choice(suits),choice(cards))
        user.append(current)
        used.append(current)
        numcards+=1
    for suit in suits:
        for card in cards:
            if (suit,card) not in user:
                comp.append((suit,card))
def place():
    if userturn and len(user)>0:
        current=user[0]
        print current
        del user[0]
        img='%s%s.bmp'%(current[0],current[1])
        card1.config(image=img)


master=Tk()
card1=Label(master,text='')
card1.pack()
card2=Label(master,text='')
card2.pack()
card3=Label(master,text='')
card3.pack()
card4=Label(master,text='')
card4.pack()
card5=Label(master,text='')
card5.pack()
play=Button(master,text='Play',command=place)
play.pack()
deal()
master.mainloop()    

Ignore the extra lines of code because those will apply to the program as I build on it more. This is just the start.

Thanks.

A common error, img is garbage collected as soon as the function exits so the image disappears as soon as it is placed on the label. You should learn classes IMHO if you are going to program GUIs. Anyway, to make it persistent you can attach to a global class instance, like card1 (and obviously we don't have the images so can't test this code).

    img_name='%s%s.bmp'%(current[0],current[1])
    img=BitmapImage(file=img_name)
    card1.img = img
    card1.config(image=card1.img)

There are two problems with your code:

  1. Inside place() , you forgot to specify the path to the folder where your images are saved . The line img='%s%s.bmp'%(current[0],current[1]) specifies only the names of your images but not the path to the folder where they exist . Thus you got that error message: TCL error saying the image doesn't exist.

  2. Once you fixed what I stated above, you still have to keep a reference to your images inside place() method by running card1.image = img

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM