-1
您好,我正在使用Tkinter製作一臺使用Python的老虎機。 代碼文件和圖像都包含一個文件夾中:_tkinter.TclError:image「Horseshoe.gif」does not exist
然而,當我運行代碼,並且圖像是所謂的,我得到的錯誤:
_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()