Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13981

First canvas goes blank when 2nd canvas is created

$
0
0

I am trying to load two different Pokemon for battle.First, it chooses a random Pokemon to fight and displays its canvas window with its hp life bar inside.Then Second, you choose your Pokemon, When that happens it loads the 2nd canvas causing the first canvas to go blank.I have tried everything to resolve this even with chatGBT and still got nothing.I don't know why the first canvas goes blank. And on top of that when you click on the canvas windows they say not responding.The Enemies Life Bar Module is the same code as the LifeBar Module(Your Pokemon HP Module)So you have LifeBar ModuleAnd you have EnemiesLifeBar Module

Enemies Life Bar Module

import tkinter as tkfrom PIL import Image, ImageTkclass EnemiesLifeBar:    def __init__(self, name, max_life, current_life):        self.image_path = ""        self.pokemon_photo = ""        self.pokemon_image = ""        self.name = name        self.max_life = max_life        self.current_life = current_life        self.root = tk.Tk()        self.root.title(name)        self.canvas = tk.Canvas(self.root, width=300, height=30, bg='white')        self.canvas.pack()        self.damage_taken = 0        self.damage_taken_done = 0        self.damage_increase = 0        self.damage_increase_done = 0        if self.damage_taken_done == 1:            self.root.after(30, self.animate_damage_taken)        if self.damage_increase_done == 1:            self.root.after(30, self.animate_health_increase)        # Load Pokémon image        self.image_path = "C:/Users/Samuel~1/Desktop/PokeMonSounds/Images/" + name +".png"        self.pokemon_image = Image.open(self.image_path)        self.pokemon_image = self.pokemon_image.resize((100, 100))        self.pokemon_photo = ImageTk.PhotoImage(self.pokemon_image)        self.pokemon_label = tk.Label(self.root, image=self.pokemon_photo)        self.pokemon_label.pack()  # Adjust as needed        # Display life bar        self.update_life_bar()    def update_life_bar(self):        bar_width = (self.current_life / self.max_life) * 300        self.canvas.delete("all")        self.canvas.create_rectangle(0, 0, bar_width, 30, fill='red')        self.canvas.create_text(150, 15, text=f"Life: {self.current_life}/{self.max_life}", fill='white')        self.root.update()    def decrease_life(self, amount):        self.current_life -= amount        if self.current_life < 0:            self.current_life = 0        self.update_life_bar()    def increase_life(self, amount):        self.current_life += amount        if self.current_life > 100:            self.current_life = 100        self.update_life_bar()    def animate_damage_taken(self):        if self.damage_taken > 0:            self.decrease_life(1)            self.root.after(30, self.animate_damage_taken)            self.damage_taken -= 1    def animate_health_increase(self):        if self.damage_increase > 0:            self.increase_life(1)            self.root.after(30, self.animate_health_increase)            self.damage_increase -= 1    def run(self):        self.root.mainloop()        self.run()

Viewing all articles
Browse latest Browse all 13981

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>