Image won't update despite variables being updated properly

5 hours ago 1
ARTICLE AD BOX

In the following snapshot, the image of the pet is supposed to be in an "Idle" state, but is currently in a "Sad" state, despite the console saying the state is currently in "Idle". I have been able to modify the original variable "State" so that it should be able to change the image to the "Idle" state, and the prints show it is in an "Idle" state

A Python GUI window displaying a Tamagotchi game, accompanied by the current prints from the same program

import random, csv import tkinter as tk from tkinter import ttk, messagebox, PhotoImage root = tk.Tk() class MyFrame(ttk.Frame): # Assets folder contain all pet assets, including frames and quotes # PlayerPets.csv contains all accounts and current stats of their pets PetStates = ["Idle", "Happy", "Sad", "Dirty", "Sleep", "Eat", "Tired", "Deny"] # All pet states State = 2 Pet = 1 # Current Pet frame = 0 # Current Frame PetFrames = [tk.PhotoImage(file="PetAssets/Pet" + str(Pet) + PetStates[State] + "1.png").zoom(4,4), tk.PhotoImage(file="PetAssets/Pet" + str(Pet) + PetStates[State] + "2.png").zoom(4,4)] # Is cycled through in function animation() fps = 750 # Time in milliseconds for root.after method Happiness = 21 Fullness = 21 Energy = 21 Hlabel = tk.StringVar() Flabel = tk.StringVar() Elabel = tk.StringVar() def __init__(self, parent): ttk.Frame.__init__(self, parent, padding="10 10 10 10") self.pack() def animation(): print(MyFrame.PetStates[MyFrame.State], self.Happiness) MyFrame.frame = (self.frame + 1) % len(self.PetFrames) canvas.itemconfig(image_id, image=self.PetFrames[self.frame]) self.Hlabel.set("Happiness: [" + "|" * self.Happiness + "· " * (25 - self.Happiness) + "]") self.Flabel.set("Fullness: [" + "|" * self.Fullness + "· " * (25 - self.Fullness) + "]") self.Elabel.set("Energy: [" + "|" * self.Energy + "· " * (25 - self.Energy) + "]") root.after(self.fps, animation) def check_state(): randomizer = random.randint(0, 100) if randomizer == 0 and self.State == 0: self.State == 3 return else: if self.Happiness < 5: MyFrame.State = 2 self.fps = 1000 return elif self.Fullness < 5: MyFrame.State = 2 self.fps = 1000 return elif self.Energy < 5: MyFrame.State = 6 self.fps = 1000 return elif self.State == 3: return elif self.State == 4: return else: MyFrame.State = 0 self.fps = 750 return ... def run_all(): animation() timepass_happiness() timepass_fullness() timepass_energy() # Creates the ui for the game itself ttk.Button(self, text="Pet").grid( column=0, row=0) ttk.Button(self, text="Feed").grid( column=1, row=0) ttk.Button(self, text="Wash").grid( column=2, row=0) ttk.Button(self, text="Exit").grid( column=3, row=0) Hlabel = ttk.Label(self, textvariable=self.Hlabel).grid( column=4, row=1, pady=20) Flabel = ttk.Label(self, textvariable=self.Flabel).grid( column=4, row=2, pady=20) Elabel = ttk.Label(self, textvariable=self.Elabel).grid( column=4, row=3, pady=20) canvas = tk.Canvas(self, width=300, height=160) canvas.place(relx=0, rely=.2) img = self.PetFrames[self.frame] image_id = canvas.create_image(150, 90, image=img, tag="frame") run_all() if __name__ == "__main__": root.title("Tamagotchi Pet Game") MyFrame(root) root.mainloop()
Read Entire Article