You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter类开发图像小说游戏时遇Label对象不可调用错误求助

Fixing "Label object is not callable" Error in Tkinter Image Novel Game

Problem Description

I'm building an image novel-style game using Tkinter (chose it due to time constraints instead of other libraries), but I hit an error when running the code. The error occurs at self.pantalla(self.root, image=self.estaes) in the A() method, with the message: "Label object is not callable". self.pantalla is defined as a Label object in the __init__ method—can someone help me figure out why this is happening?

My Code

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()

Error Message

Label object is not callable

Solution

Let's break down the issues and fix them step by step:

1. The Root Cause of the "Not Callable" Error

In your __init__ method, you initialize self.pantalla = tk.Label()—this creates a Label instance, not a reference to the Label class. Later, in the A() method, you try to call this instance like a function: self.pantalla(self.root, image=self.estaes). That's why you get the "not callable" error—instances can't be invoked as constructors.

Instead, you need to either:

  • Reassign self.pantalla to a new Label instance, or
  • Update the existing Label's image using the config() method.

2. Fixing the A() Method

Here's the corrected version of the A() method (we'll reassign a new Label instance, since you're already using forget()):

def A(self):
    self.pantalla.forget()
    self.image = Image.open("salir.png")
    self.estaes = ImageTk.PhotoImage(self.image)
    # Reassign self.pantalla to a new Label instance (not call the existing one)
    self.pantalla = tk.Label(self.root, image=self.estaes)
    self.pantalla.place(x=0, y=0)
    
    # Fix button creation too (you had the same "not callable" issue here)
    self.boton1.destroy()
    self.boton2.destroy()
    self.boton1 = tk.Button(self.root, text="Funciona?")
    self.boton2 = tk.Button(self.root, text="Funciona!")
    self.boton1.pack(side="bottom")
    self.boton2.pack(side="bottom")

3. Fixing Button Callback Issues in comenzar()

You also have a mistake in how you set up button commands:

  • command=self.A() will execute the A() method immediately when the button is created, not when it's clicked. Use command=self.A (no parentheses) instead.
  • command=print("B") has the same problem—use a lambda to delay execution until the button is clicked.

Corrected comenzar() button code:

self.boton1 = tk.Button(self.root, text="A: Salir a la calle", command=self.A)
self.boton2 = tk.Button(self.root, text="B: Quedarse en la casa", command=lambda: print("B"))

Key Takeaways

  • Once you create an instance of a Tkinter widget (like Label or Button), you can't call that instance as if it were the class itself—reassign it to a new instance or update its properties with config().
  • When setting button command parameters, pass the method/function reference (no parentheses) unless you need to pass arguments, in which case use a lambda.

内容的提问来源于stack exchange,提问作者MiBgunst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:47:39