python - 错误:图像“pyimage1”不存在(pt2)是 python 的新手 btw

我需要帮助创建带有图像的按钮,图像的创建似乎不是问题,因为当我将鼠标悬停在图像的文件位置时,图像会弹出。但是,屏幕截图中显示了错误图像“pyimage1”不存在。这是一个用于学校项目的 cookie 点击器类型游戏,我对 Python 非常了解(到目前为止学习了 3 天)。导入操作系统并使用 OS.cwd() 无效,因为文件路径正确。这是我的代码:

from tkinter import *
import os

from Game import Game

gui = Tk()
gui.title("Clicking Mania")
gui.geometry("600x600")

# Inside GUI title
title = Label(gui, text="Clicking Mania")
title.pack()
title.grid_location(3, 2)

# Images
os.getcwd()
image = PhotoImage(file='../GameImages/DPic.png')
imagelabel = Label(image=image)
imageshop = PhotoImage(file='../GameImages/upgrade.png')
imageupgrade = Label(image=imageshop)

# Buttons
pickaxebutton = Button(gui, image=image, command=Game.clickcmd(1), borderwidth=0)
pickaxebutton.pack(pady=100)
upgradebutton = Button(gui, image=imageshop, borderwidth=0)

# change image
up1button = Button(gui, image=image, command=Game.up1cmd(), borderwidth=0)

balance = Label(gui, text="Balance: ", padx=20, pady=10)
balance.pack()

gui.mainloop()
class Game():
    multiplier = 0

    global upgrades
    upgrades = ["2","4","6","8","10"],["Upgrade 1", "Upgrade 2", "Upgrade 3", "Upgrade 4", "Upgrade 5"]



    def setMultiplier(x):
        multiplier = x
        print(multiplier)


    def clickcmd(multiplier):
        global clicks
        clicks += 1 * (multiplier)
        return clicks



    def up1cmd(x):
        setMultiplier(upgrades[0][x])





    # x = Which upgrade was bought, returns upgrade[x]
    def getUpgrade(x):
        return upgrades[0,x] , upgrades[1,x]

回答1

您的 Game 类中有许多尚未发现的问题。 Python 中的二维数组不被索引为 [a,b]——它必须是 [a][b]。您不要将 global 与类变量一起使用。并且您必须使用 self 来访问对象变量。看起来您希望将第一个列表用作整数,因此您需要将它们设为整数。请注意,对于 upgrades,字典可能是更好的选择:

class Game:
    multiplier = 0
    clicks = 0
    upgrades = [2,4,6,8,10],["Upgrade 1", "Upgrade 2", "Upgrade 3", "Upgrade 4", "Upgrade 5"]

    def setMultiplier(self, x):
        self.multiplier = x
        print(self.multiplier)

    def clickcmd(self, multiplier):
        self.clicks += multiplier
        return self.clicks

    def up1cmd(self, x):
        self.setMultiplier(self.upgrades[0][x])

    # x = Which upgrade was bought, returns upgrade[x]
    def getUpgrade(self, x):
        return self.upgrades[0][x] , self.upgrades[1][x]

相似文章

java - 使用 Java 将日志解析为 csv

我正在尝试使用给定的正则表达式模式解析我的日志文件并导出到csv,但是数据输入发生了变化,我怎样才能准确地找到模式并写入csv文件。模式:(PT.a=.PT=b,...)以&结尾日志文件:2022-0...

最新文章