'_tkinter.TclError: image "pyimage1" doesn't exist, I am new to python
I need help with creating a button with an image, the creation of the image seems to not be a problem as when I hover over the file location of the image, the image pops up. However, the error image "pyimage1" doesn't exist shows up as seen in the screenshots. This is for a cookie clicker type game for a school project and I am very knew to Python (3 days of learning so far).
from tkinter import *
gui = Tk()
clicks = 0
class Game():
multiplier = 0
global upgrades
upgrades = ["2", "4", "6", "8", "10", "20"], ["Upgrade 1", "Upgrade 2", "Upgrade 3", "Upgrade 4", "Upgrade 5", "Maxed"]
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]
`
from tkinter import *
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
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()
Solution 1:[1]
Hard to tell with just seeing a couple lines of your code, but most likely the path to the image is wrong. Try using the OS library to print out your current path.
import os
os.getcwd()
Then from there you can figure out what path you need to give. Or you can give an absolute path to see if that is the issue or not.
Solution 2:[2]
If it's a matter of life and death, I would write the data merging function in C. This is the only way to be 100% sure of what happens with the data. The general philosophy of Python is to hide whatever happens "under the hood", this does not seem to fit your particular use case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Do Geese See God |
| Solution 2 | user23952 |
