2015-09-29 79 views
-1

您好,我正在使用Tkinter製作一臺使用Python的老虎機。 代碼文件和圖像都包含一個文件夾中:_tkinter.TclError:image「Horseshoe.gif」does not exist

enter image description here

然而,當我運行代碼,並且圖像是所謂的,我得到的錯誤:

_tkinter.TclError: image "imagename.gif" doesn't exist 

這裏是我的整個代碼:

from tkinter import * 
from random import * 

photoList = ["7.gif","Apple.gif","Bell.gif","Clover.gif","Diamond.gif","Grape.gif","Horseshoe.gif","Pear.gif","Strawberry.gif"] 

slotMachine = Tk() 

file = open("credit.txt", mode = "w") 
file.write("50") 
file.close() 

def getMoney(): 
    file = open("credit.txt", mode = "r") 
    lst = file.readlines() 
    money = int(lst[0].strip("\n")) 
    file.close() 
    file = open("credit.txt", mode = "w") 
    file.write(str(money-5)) 
    file.close() 
    return money 

def addMoney(add): 
    money = getMoney() + add 
    file = open("credit.txt", mode = "w") 
    file.write(str(money)) 
    file.close() 
    lbl5.config(text = "Money: £{0}".format(money)) 

def spin(): 
    credit = getMoney() 
    if credit > 0: 
     credit = credit - 5 
     lbl5.config(text = "Money: £{0}".format(credit)) 
     slot1 = photoList[randint(0,8)] 
     slot2 = photoList[randint(0,8)] 
     slot3 = photoList[randint(0,8)] 
     canvas1.create_image(26,26, image = slot1) 
     canvas2.create_image(26,26, image = slot2) 
     canvas3.create_image(26,26, image = slot3) 
    else: 
     lbl5.config(text = "Out Of Credit!") 

    if slot1 == slot2 or slot2 == slot3 or slot1 == slot3: 
     lbl6.config(text = "Double Match! Winnings: £5") 
     addMoney(5) 
    elif slot1 == slot2 and slot2 == slot3: 
     lbl6.config(text = "Full House! Winnings: £20") 
     addMoney(20) 
    else: 
     lbl6.config(text = "No Win! Try Again!") 

lbl1 = Label(slotMachine, text = "Welcome to the Slot Machine!", font = ("Calibri", 16)) 
lbl1.grid(row = 0, column = 0) 
lbl1.pack() 

canvas1 = Canvas(slotMachine, height = 52, width = 52) 
##canvas1.grid(row = 1, column = 0) 

canvas2 = Canvas(slotMachine, height = 52, width = 52) 
##canvas2.grid(row = 1, column = 1) 

canvas3 = Canvas(slotMachine, height = 52, width = 52) 
##canvas3.grid(row = 1, column = 2) 

photo1 = PhotoImage(file = "x.gif") 

canvas1.create_image(26,26, image = photo1) 
canvas2.create_image(26,26, image = photo1) 
canvas3.create_image(26,26, image = photo1) 

lbl5 = Label(slotMachine, text = "Money: £50") 
lbl5.grid(row = 2, column = 0) 
lbl5.pack() 

spinBtn = Button(slotMachine, text = "Spin - £5", command = spin) 
spinBtn.grid(row = 2, column = 1) 
spinBtn.pack() 

lbl6 = Label(slotMachine, text = "", font = ("Calibri", 16)) 
lbl6.grid(row = 3, column = 0) 
lbl6.pack() 

slotMachine.mainloop() 

回答

0

考慮以下代碼:

photoList = ["7.gif","Apple.gif","Bell.gif",...] 
... 
slot1 = photoList[randint(0,8)] 
... 
canvas1.create_image(26,26, image = slot1) 

photoList是文件名列表。當你創建圖像時,你會給它一個文件名。這不是你在畫布上創建圖像的方式。您必須使用文件名創建一個PhotoImage實例,然後將該實例傳遞給create_image方法。

由於您不止一次使用這些圖像,因此首先將所有文件名轉換爲圖像並將圖像存儲在photoList而非存儲圖像的路徑是有意義的。這樣,您就不必更改創建圖像的代碼。

注意:除了這個問題,你還得到了一個程序,你不斷在畫布上創建圖像在同一個地方,將它們堆疊在一起。儘管需要一段時間,但最終你會耗盡內存。您應該銷燬舊圖像或重新使用它們。

相關問題