2011-12-28 113 views
0

這是我正在嘗試顯示圖像的代碼。 那麼..首先應該有4張牌在屏幕上顯示;然而,只有2. 我不知道爲什麼會發生這種情況。我真的需要幫助。Tkinter顯示圖像

def __init__(self): 
    Frame.__init__(self) 
    self.master.title("Black Jack") 
    blackjack = Blackjack() 
    self.grid() 

    self._playerCardImage = PhotoImage(file=str(blackjack._player._cards[0].fileName)) 
    self._playerCardLabel = Label(self, image = self._playerCardImage) 
    self._playerCardLabel.grid(row=0, column=0) 


    self._playerCardImage1 = PhotoImage(file=str(blackjack._player._cards[1].fileName)) 
    self._playerCardLabel1 = Label(self, image = self._playerCardImage1) 
    self._playerCardLabel1.grid(row=0, column=1) 

    self._dealerCardImage = PhotoImage(file=str(blackjack._dealer._cards[0].fileName)) 
    self._dealerCardImage = Label(self, image = self._dealerCardImage) 
    self._dealerCardImage.grid(row=0, column=2) 

    self._dealerCardImage1 = PhotoImage(file=str(blackjack._dealer._cards[1].fileName)) 
    self._dealerCardImage1 = Label(self, image = self._dealerCardImage1) 
    self._dealerCardImage1.grid(row=0, column=3) 

回答

4

你的第一個兩行代碼爲第2卡:

self._playerCardImage = PhotoImage(file=str(blackjack._player._cards[0].fileName)) 
self._playerCardLabel = Label(self, image = self._playerCardImage) 
self._playerCardLabel.grid(row=0, column=0) 

你的最後兩行代碼的最後2張牌:

self._dealerCardImage1 = PhotoImage(file=str(blackjack._dealer._cards[1].fileName)) 
self._dealerCardImage1 = Label(self, image = self._dealerCardImage1) 
self._dealerCardImage1.grid(row=0, column=3) 

你重新初始化_dealerCardImage1作爲標籤。
您需要將ALST 2行更改爲類似:

self._dealerCardImage1 = PhotoImage(file=str(blackjack._dealer._cards[1].fileName)) 
self._dealerCardLabel1 = Label(self, image = self._dealerCardImage1) 
self._dealerCardLabel1.grid(row=0, column=3) 
+0

哦,天哪..我的壞。粗心的錯誤!謝謝! – user1047092 2011-12-28 05:38:32

+2

@ user1047092沒問題:),還記得標記答案,它會讓人想要更多的幫助你。 – Serdalis 2011-12-28 05:39:52

+0

我只是想了另一個問題。現在我正在嘗試顯示4張圖片。有沒有辦法顯示圖像,而不必像上面寫的重複代碼? – user1047092 2011-12-28 05:55:50

1

在另一個答案你問,如果你能創造一個循環的圖像的問題。沒有什麼可以阻止你這樣做。

例如,如果你想創建4張球員卡,你可以這樣做:

def __init__(self): 
    Frame.__init__(self) 
    self.master.title("Black Jack") 
    blackjack = Blackjack() 
    self.grid() 

    self._images = [] 
    self._labels = [] 
    for column in range(4): 
     image = PhotoImage(file=str(blackjack._player._cards[column].fileName)) 
     label = Label(self, image = image) 
     label.grid(row=0, column=column) 

     self._images.append(image) 
     self._labels.append(label)