0

I'm using the tkinter GUI to call images and buttons to make a sorta "graphic novel" game (Probably not the most appropiate, but I don't have enough time to look for and use another library). Relevant code attached:

import random
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk

class juego:
    def __init__(self):
        self.root = tk.Tk()
        self.nombreimagen = "room.jpg"
        self.strparaA = " "
        self.strparaB = " "
        self.comparaA = " "
        self.comparaB = " "
        self.boton1 = tk.Button()
        self.boton2 = tk.Button()
        self.image = Image.open(self.nombreimagen)
        self.estaes = ImageTk.PhotoImage(self.image)
        self.pantalla = tk.Label()



    def comenzar(self):
        self.nombreimagen = "room.jpg"
        self.root.geometry("960x720")
        self.image = Image.open(self.nombreimagen)
        self.estaes = ImageTk.PhotoImage(self.image)
        self.pantalla = tk.Label(self.root, image=self.estaes)
        self.pantalla.place(x=0, y=0)        
        self.boton1(self.root, text="A: Salir a la calle", command=self.A())
        self.boton2(self.root, text="B: Quedarse en la casa", command=print("B"))
        self.boton1.pack(side="bottom")
        self.boton2.pack(side="bottom")
        self.root.mainloop()

    def A(self):
        self.pantalla.forget()
        self.image = Image.open("salir.png")
        self.estaes = ImageTk.PhotoImage(self.image)
        self.pantalla(self.root, image=self.estaes)
        self.pantalla.place(x=0, y=0)
        self.boton1(self.root, text="Funciona?")
        self.boton2(self.root, text="Funciona!")
        self.boton1.pack(side="bottom")
        self.boton2.pack(side="bottom")

nombre = juego()
nombre.comenzar()

Where "comenzar" is the function for drawing the game's starting window. When I execute it I get the error "Label object is not callable" in line

self.pantalla(self.root, image=self.estaes)

self.pantalla should be the label defined in the ____init____

MiBgunst
  • 11
  • 2
  • You can only call the `Label` class to construct an object but you can't call the object. – Michael Butscher May 19 '19 at 03:38
  • 1
    if you want new label then you have to use `self.pantalla = tk.Label(self.root, image=self.estaes)`. But you can also change existing label - `self.pantalla["image"] = self.estaes` and then you don't need `self.pantalla.forget()` which only hide widget, not remove from memory. – furas May 19 '19 at 03:46
  • That sounds somewhat useful. The reason why I want to use `forget()` is because trying to pack a new image defined with another name, Tkinter throws an error, so I just want to update the existing instance of it by making it read a new file. Is there a way I can do that as well? – MiBgunst May 19 '19 at 03:54
  • @MiBgunst *"trying to pack a new image"*: You **don't** `pack` images, you have to layout using e.g. `pack` any widget, here `Label`. As @ furas wrote, to change the image of an existing `Label` use `self.pantalla["image"] = self.estaes`. Relevant [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/a/16424553/7414759) – stovfl May 19 '19 at 06:59

0 Answers0