Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-找不到Tkinter PhotoImage图像_Python_Tkinter_Photoimage - Fatal编程技术网

Python-找不到Tkinter PhotoImage图像

Python-找不到Tkinter PhotoImage图像,python,tkinter,photoimage,Python,Tkinter,Photoimage,我正在为21点游戏编写代码。所有的.gif都在.py文件目录中,当直接引用时,它们可以正常工作。但是,当我使用tkinter下面的行报告时: _tkinter.TclError: image "SixDiamonds.gif" doesn't exist 我正在尝试获取卡名并使用以下代码将其插入函数deal(): w.create_image(player_coords, image=card+".gif") 但是,当它说这些文件不存在的时候。如果我将行更改为特定的卡图像: w.create

我正在为21点游戏编写代码。所有的.gif都在.py文件目录中,当直接引用时,它们可以正常工作。但是,当我使用tkinter下面的行报告时:

_tkinter.TclError: image "SixDiamonds.gif" doesn't exist
我正在尝试获取卡名并使用以下代码将其插入函数deal():

w.create_image(player_coords, image=card+".gif")
但是,当它说这些文件不存在的时候。如果我将行更改为特定的卡图像:

w.create_image(player_coords, image="AceSpades.gif")
例如,它将起作用。但是,我需要根据所绘制的卡片引用它。任何帮助都将不胜感激

from tkinter import *
import tkinter.messagebox as messagebox

money = "0"
player = []
playervalue = 0
dealer = []
dealervalue = 0
in_play = True
not_dealt = True

root = Tk()
w = Canvas(root, height=500, width=750, bg="dark green")
card_back = PhotoImage(file="card_back.gif")
AceSpades = PhotoImage(file="AceSpades.gif")
TwoSpades = PhotoImage(file="TwoSpades.gif")
ThreeSpades = PhotoImage(file="ThreeSpades.gif")
FourSpades = PhotoImage(file="FourSpades.gif")
FiveSpades = PhotoImage(file="FiveSpades.gif")
SixSpades = PhotoImage(file="SixSpades.gif")
SevenSpades = PhotoImage(file="SevenSpades.gif")
EightSpades = PhotoImage(file="EightSpades.gif")
NineSpades = PhotoImage(file="NineSpades.gif")
TenSpades = PhotoImage(file="TenSpades.gif")
JackSpades = PhotoImage(file="JackSpades.gif")
QueenSpades = PhotoImage(file="QueenSpades.gif")
KingSpades = PhotoImage(file="KingSpades.gif")
AceHearts = PhotoImage(file="AceHearts.gif")
TwoHearts = PhotoImage(file="TwoHearts.gif")
ThreeHearts = PhotoImage(file="ThreeHearts.gif")
FourHearts = PhotoImage(file="FourHearts.gif")
FiveHearts = PhotoImage(file="FiveHearts.gif")
SixHearts = PhotoImage(file="SixHearts.gif")
SevenHearts = PhotoImage(file="SevenHearts.gif")
EightHearts = PhotoImage(file="EightHearts.gif")
NineHearts = PhotoImage(file="NineHearts.gif")
TenHearts = PhotoImage(file="TenHearts.gif")
JackHearts = PhotoImage(file="JackHearts.gif")
QueenHearts = PhotoImage(file="QueenHearts.gif")
KingHearts = PhotoImage(file="KingHearts.gif")
AceClubs = PhotoImage(file="AceClubs.gif")
TwoClubs = PhotoImage(file="TwoClubs.gif")
ThreeClubs = PhotoImage(file="ThreeClubs.gif")
FourClubs = PhotoImage(file="FourClubs.gif")
FiveClubs = PhotoImage(file="FiveClubs.gif")
SixClubs = PhotoImage(file="SixClubs.gif")
SevenClubs = PhotoImage(file="SevenClubs.gif")
EightClubs = PhotoImage(file="EightClubs.gif")
NineClubs = PhotoImage(file="NineClubs.gif")
TenClubs = PhotoImage(file="TenClubs.gif")
JackClubs = PhotoImage(file="JackClubs.gif")
QueenClubs = PhotoImage(file="QueenClubs.gif")
KingClubs = PhotoImage(file="KingClubs.gif")
AceDiamonds = PhotoImage(file="AceDiamonds.gif")
TwoDiamonds = PhotoImage(file="TwoDiamonds.gif")
ThreeDiamonds = PhotoImage(file="ThreeDiamonds.gif")
FourDiamonds = PhotoImage(file="FourDiamonds.gif")
FiveDiamonds = PhotoImage(file="FiveDiamonds.gif")
SixDiamonds = PhotoImage(file="SixDiamonds.gif")
SevenDiamonds = PhotoImage(file="SevenDiamonds.gif")
EightDiamonds = PhotoImage(file="EightDiamonds.gif")
NineDiamonds = PhotoImage(file="NineDiamonds.gif")
TenDiamonds = PhotoImage(file="TenDiamonds.gif")
JackDiamonds = PhotoImage(file="JackDiamonds.gif")
QueenDiamonds = PhotoImage(file="QueenDiamonds.gif")
KingDiamonds = PhotoImage(file="KingDiamonds.gif")

player_coords = (75, 360)
player_coords2 = (175, 360)
dealer_coords = (75, 140)
dealer_coords2 = (175, 140)


def create_deck():
    createdeck = {"AceSpades": 1, "TwoSpades": 2, "ThreeSpades": 3, "FourSpades": 4, "FiveSpades": 5, "SixSpades": 6,
                  "SevenSpades": 7, "EightSpades": 8, "NineSpades": 9, "TenSpades": 10, "JackSpades": 10,
                  "QueenSpades": 10, "KingSpades": 10, "AceHearts": 1, "TwoHearts": 2, "ThreeHearts": 3,
                  "FourHearts": 4, "FiveHearts": 5, "SixHearts": 6, "SevenHearts": 7, "EightHearts": 8, "NineHearts": 9,
                  "TenHearts": 10, "JackHearts": 10, "QueenHearts": 10, "KingHearts": 10, "AceClubs": 1, "TwoClubs": 2,
                  "ThreeClubs": 3, "FourClubs": 4, "FiveClubs": 5, "SixClubs": 6, "SevenClubs": 7, "EightClubs": 8,
                  "NineClubs": 9, "TenClubs": 10, "JackClubs": 10, "QueenClubs": 10, "KingClubs": 10, "AceDiamonds": 1,
                  "TwoDiamonds": 2, "ThreeDiamonds": 3, "FourDiamonds": 4, "FiveDiamonds": 5, "SixDiamonds": 6,
                  "SevenDiamonds": 7, "EightDiamonds": 8, "NineDiamonds": 9, "TenDiamonds": 10, "JackDiamonds": 10,
                  "QueenDiamonds": 10, "KingDiamonds": 10}
    return createdeck


def deal():
    global playervalue, dealervalue, in_play, not_dealt
    if in_play and not_dealt:
        # add inital player cards
        card, value = deck.popitem()
        player.append(card)
        playervalue += value
        w.create_image(player_coords, image=card+".gif")
        card, value = deck.popitem()
        player.append(card)
        playervalue += value
        # add initial dealer cards
        card, value = deck.popitem()
        dealer.append(card)
        dealervalue += value
        w.create_image(dealer_coords, image=card_back)
        card, value = deck.popitem()
        dealer.append(card)
        dealervalue += value
        w.create_image(dealer_coords2, image=card_back)
        print("Dealer", dealer, dealervalue)
        print("Player", player, playervalue)
        not_dealt = False


def hit():
    global playervalue, dealervalue, in_play
    if in_play:
        card, value = deck.popitem()
        player.append(card)
        playervalue += value
        print("Your current hand is", player)
        print(playervalue, "\n")
        if playervalue > 21:
            messagebox.showinfo(title="Outcome", message="You bust and lose!", parent=w)
            print("The dealer had", dealervalue, dealer, "\n")


def stand():
    global dealervalue, in_play
    if in_play:
        while dealervalue < 17:
            card, value = deck.popitem()
            dealer.append(card)
            dealervalue += value
            print("Dealer deals himself a card.\n")
        if dealervalue < 18 > 20:
            print("Dealer is staying.\n")
        scoring()


def scoring():
    global in_play
    value = str("0")
    print("Dealer score:", dealervalue)
    print("Dealers hand was", dealer, "\n")
    print("Player score:", playervalue, "\n")
    if dealervalue == 21:
        value = str("Dealer has 21, you lose!")
    elif playervalue == 21:
        value = str("You have 21, you win!")
    elif dealervalue == 21 and playervalue == 21:
        value = str("Tie! No one wins.")
    elif dealervalue > 21 and playervalue > 21:
        value = str("You both bust!")
    elif playervalue < dealervalue < 21 and playervalue < 21:
        value = str("Dealer wins.")
    elif dealervalue < playervalue and dealervalue < 21 and playervalue < 21:
        value = str("You win! Dealer loses.")
    elif dealervalue > 21:
        value = str("Dealer busts, you win!")
    messagebox.showinfo(title="Outcome", message=value, parent=w, command=root.destroy)
    in_play = False


def main():
    root.title("Blackjack")
    w.pack()
    bjbutton1 = Button(root, text="Hit", highlightbackground="dark green", command=hit)
    w.create_window(40, 480, window=bjbutton1)
    bjbutton2 = Button(root, text="Stand", highlightbackground="dark green", command=stand)
    w.create_window(100, 480, window=bjbutton2)
    bjbutton3 = Button(root, text="Deal", highlightbackground="dark green", command=deal)
    w.create_window(165, 480, window=bjbutton3)
    bjbutton4 = Button(root, text="Exit", command=root.destroy, highlightbackground="dark green")
    w.create_window(715, 480, window=bjbutton4)
    w.create_text(680, 450, text="Money: " + str(money), fill="white", justify="right",
                  font=("Brush Script MT Italic", "20"))
    w.create_text(50, 25, text="Dealer: ", fill="white", justify="right", font=("Brush Script MT Italic", "20"))
    w.create_text(50, 250, text="Player: ", fill="white", justify="right", font=("Brush Script MT Italic", "20"))
    w.mainloop()


deck = create_deck()
main()
从tkinter导入*
将tkinter.messagebox作为messagebox导入
money=“0”
玩家=[]
playervalue=0
经销商=[]
DealValue=0
在游戏中=正确
不正确
root=Tk()
w=画布(根,高度=500,宽度=750,bg=“深绿色”)
card\u back=PhotoImage(file=“card\u back.gif”)
AceSpades=PhotoImage(file=“AceSpades.gif”)
TwoSpades=PhotoImage(file=“TwoSpades.gif”)
ThreeSpades=PhotoImage(file=“ThreeSpades.gif”)
FourSpades=PhotoImage(file=“FourSpades.gif”)
FiveSpades=PhotoImage(file=“FiveSpades.gif”)
SixSpades=PhotoImage(file=“SixSpades.gif”)
SevenSpades=PhotoImage(file=“SevenSpades.gif”)
EightSpades=PhotoImage(file=“EightSpades.gif”)
NineSpades=PhotoImage(file=“NineSpades.gif”)
TenSpades=PhotoImage(file=“TenSpades.gif”)
JackSpades=PhotoImage(file=“JackSpades.gif”)
QueenSpades=PhotoImage(file=“QueenSpades.gif”)
KingSpades=PhotoImage(file=“KingSpades.gif”)
aceharts=PhotoImage(file=“aceharts.gif”)
TwoHearts=PhotoImage(file=“TwoHearts.gif”)
ThreeHearts=PhotoImage(file=“ThreeHearts.gif”)
FourHearts=PhotoImage(file=“FourHearts.gif”)
FiveHearts=PhotoImage(file=“FiveHearts.gif”)
SixHearts=PhotoImage(file=“SixHearts.gif”)
SevenHearts=PhotoImage(file=“SevenHearts.gif”)
eightheart=PhotoImage(file=“eightheart.gif”)
NineHearts=PhotoImage(file=“NineHearts.gif”)
TenHearts=PhotoImage(file=“TenHearts.gif”)
JackHearts=PhotoImage(file=“JackHearts.gif”)
Queenherts=PhotoImage(file=“queenherts.gif”)
Kingharts=PhotoImage(file=“kingharts.gif”)
AceClubs=PhotoImage(file=“AceClubs.gif”)
TwoClubs=PhotoImage(file=“TwoClubs.gif”)
ThreeClubs=PhotoImage(file=“ThreeClubs.gif”)
FourClubs=PhotoImage(file=“FourClubs.gif”)
FiveClubs=PhotoImage(file=“FiveClubs.gif”)
SixClubs=PhotoImage(file=“SixClubs.gif”)
SevenClubs=PhotoImage(file=“SevenClubs.gif”)
EightClubs=PhotoImage(file=“EightClubs.gif”)
NineClubs=PhotoImage(file=“NineClubs.gif”)
TenClubs=PhotoImage(file=“TenClubs.gif”)
JackClubs=PhotoImage(file=“JackClubs.gif”)
QueenClubs=PhotoImage(file=“QueenClubs.gif”)
KingClubs=PhotoImage(file=“KingClubs.gif”)
acedommonds=PhotoImage(file=“acedommonds.gif”)
TwoDiamonds=PhotoImage(file=“TwoDiamonds.gif”)
ThreeDiamonds=PhotoImage(file=“ThreeDiamonds.gif”)
FourDiamonds=PhotoImage(file=“FourDiamonds.gif”)
FiveDiamonds=PhotoImage(file=“FiveDiamonds.gif”)
SixDiamonds=PhotoImage(file=“SixDiamonds.gif”)
SevenDiamonds=PhotoImage(file=“SevenDiamonds.gif”)
EightDiamonds=PhotoImage(file=“EightDiamonds.gif”)
NineDiamonds=PhotoImage(file=“NineDiamonds.gif”)
TenDiamonds=PhotoImage(file=“TenDiamonds.gif”)
JackDiamonds=PhotoImage(file=“JackDiamonds.gif”)
QueenDiamonds=PhotoImage(file=“QueenDiamonds.gif”)
KingDiamonds=PhotoImage(file=“KingDiamonds.gif”)
玩家坐标=(75360)
玩家合作2=(175360)
经销商协调=(75140)
经销商合作2=(175140)
def create_deck():
createdeck={“AceSpades”:1,“TwoSpades”:2,“ThreeSpades”:3,“FourSpades”:4,“FiveSpades”:5,“SixSpades”:6,
“七节”:7,“八节”:8,“九节”:9,“十节”:10,“十节”:10,
“皇后帕兹”:10,“国王黑桃”:10,“王心”:1,“两颗心”:2,“三颗心”:3,
“四心”:4,“五心”:5,“六心”:6,“七心”:7,“八心”:8,“九心”:9,
“十颗心”:10颗,“JackHearts”:10颗,“Queenharts”:10颗,“Kingharts”:10颗,“AceClubs”:1颗,“TwoClubs”:2颗,
“三俱乐部”:3,“四俱乐部”:4,“五俱乐部”:5,“六俱乐部”:6,“七俱乐部”:7,“八俱乐部”:8,
“NineClubs”:9,“TenClubs”:10,“JackClubs”:10,“QueenClubs”:10,“KingClubs”:10,“Acedmonds”:1,
“两颗钻石”:2,“三颗钻石”:3,“四颗钻石”:4,“五颗钻石”:5,“六颗钻石”:6,
“七颗钻石”:7颗,“八颗钻石”:8颗,“九颗钻石”:9颗,“十颗钻石”:10颗,
“皇后钻石”:10,“国王钻石”:10}
返回createdeck
def deal():
全球播放价值,DealValue,播放中,未播放
如果在游戏中且未交易:
#添加初始玩家卡
卡片,值=deck.popitem()
玩家。附加(卡)
playervalue+=值
w、 创建图像(播放器坐标,图像=卡+“.gif”)
卡片,值=deck.popitem()
玩家。附加(卡)
playervalue+=值
#添加初始经销商卡
卡片,值=deck.popitem()
经销商。附加(卡)
DealValue+=值
w、 创建镜像(经销商协作,镜像=卡片返回)
卡片,值=deck.popitem()
经销商。附加(卡)
DealValue+=值
w、 创建镜像(经销商协作2,镜像=卡片返回)
打印(“经销商”、经销商、经销商价值)
打印(“玩家”、“玩家”、“玩家”值)
不成功=错误
def hit():
全球播放价值,DealValue,在线播放
如果在游戏中:
卡片,值=deck.popitem()
玩家。附加(卡)
playervalue+=值
打印(“当前手牌是”,玩家)
打印(播放值“\n”)
如果播放值>21:
showinfo(title=“output”,message=“You bust and lose!”,parent=w)
打印(“经销商有”,经销商价值,经销商,“\n”)
def stand():
全球DealValue,在用
如果在游戏中:
当DealValue<17时:
卡片,值=deck.popitem()
the_image = PhotoImage(file="AceSpades.gif")
w.create_image(player_coords, image=the_image)
cards = ("AceSpades","TwoSpades", ...)
images = {}
for cardname in cards:
    filename = cardname + ".gif"
    images[cardname] = PhotoImage(file=filename)