python-3.x gui零未找到图像

t98cgbkg  于 2023-03-24  发布在  Python
关注(0)|答案(1)|浏览(151)

我在一个程序,将随机选择和显示5塔罗牌工作.可悲的是,我一直得到这个错误(***GUIZERO错误***图像导入错误- '[Errno 2]没有这样的文件或目录:检查文件路径和图像类型是GIF/动画GIF/BMP/ICO/PNG/JPG/TIF).所有的图像都在完全相同的目录作为脚本文件IM运行,所以我不知道该怎么办.使用完整的路径是不是一个选项,因为我希望这个项目能够在不同的位置工作.如果你们能给予我一些指针,那将是伟大的!
我的代码:

`from random import*
from guizero import App, Picture, PushButton
#### lists and other variables ####
cards = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
drawn_cards = []
card1 = ()
card2 = ()
card3 = ()
card4 = ()
card5 = ()
#### functions ####

def display_cards():
    pic1 = Picture(app, image =(card1+".png"),align="top")
    pic2 = Picture(app, image =(card2+".png"),align="left")
    pic3 = Picture(app, image =(card3+".png"),align="left")
    pic4 = Picture(app, image =(card4+".png"),align="left")
    pic5 = Picture(app, image =(card5+".png"),align="bottom")

def card_drawer():
    global cards
    global drawn_cards
    global card1
    global card2
    global card3
    global card4
    global card5
    loops = 0
    while loops < 5:
        drawn = (choice(cards))
        drawn_cards.append(drawn)
        cards.remove (drawn)
        loops = loops + 1
    card1 = (drawn_cards[0])
    card2 = (drawn_cards[1])
    card3 = (drawn_cards[2])
    card4 = (drawn_cards[3])
    card5 = (drawn_cards[4])
    card1 = str(card1)
    card2 = str(card2)
    card3 = str(card3)
    card4 = str(card4)
    card5 = str(card5)
    display_cards()

    
#### gui stuff ####
app = App()
reading = PushButton(app, text = "draw cards",align="bottom", command = card_drawer)`
ctehm74n

ctehm74n1#

事实证明,windows认为名称为1. png的文件命名为整数而不是字符串,我将文件的编号切换为罗马数字,它起作用了。

相关问题