user15702950
user15702950

Reputation:

'Button' object is not callable

I'm learning tkinter from youtube, To be more precise, it's from freecodecamp, but here I have one problem which is "'Button' object is not callable", even though I've been following it from the beginning, and here I don't know where the problem code is.

thank you

#menambah icon gambar tombol exit, dll
from tkinter import *
from PIL import ImageTk,Image

root = Tk()
root.title('Study Tkinter')

root.iconbitmap('inst.png') #add icon


my_image1=ImageTk.PhotoImage(Image.open('unsplash1.jpg')) #add photo
my_image2=ImageTk.PhotoImage(Image.open('unsplash2.jpg'))
my_image3=ImageTk.PhotoImage(Image.open('unsplash3.jpg'))
my_image4=ImageTk.PhotoImage(Image.open('unsplash4.jpg'))
my_image5=ImageTk.PhotoImage(Image.open('unsplash5.jpg'))

image_list=[my_image1,my_image2,my_image3,my_image4,my_image5]

my_label=Label(image=my_image1)
my_label.grid(row=0,column=0,columnspan=3)

def forward_button(image_number):
    global my_label
    global forward_button
    global back_button

    my_label.grid_forget()
    my_label=Label(image=image_list[image_number-1])
    forward_button =Button(root,text=">>",command=lambda:forward_button(image_number+1))
    back_button=Button(root,text="<<",command=lambda:back_button(image_number-1))

    if image_number == 5:
        forward_button=Button(root, text=">>", state=DISABLED)

    my_label.grid(row=0,column=0,columnspan=3)
    back_button.grid(row=1,column=0)
    forward_button.grid(row=1, column=2)

def back_button():
    global my_label
    global forward_button
    global back_button


back_button=Button(root,text="<<",command=back_button)
exit_button=Button(root,text="Exit Program",command=root.quit)
forward_button=Button(root,text=">>",command=lambda:forward_button(2)) #ERROR


back_button.grid(row=1,column=0)
exit_button.grid(row=1,column=1)
forward_button.grid(row=1, column=2)



root.mainloop()

Upvotes: 0

Views: 154

Answers (2)

Maciej Fender
Maciej Fender

Reputation: 331

You've declared function forward_button and in it you declared variable so Python is taking last definition which is tkinter.Button, not your defined function. Fix names and it will run fine. Below marked places where's problem

Fixed code:

#menambah icon gambar tombol exit, dll
from tkinter import *
from PIL import ImageTk,Image

root = Tk()
root.title('Study Tkinter')

root.iconbitmap('inst.png') #add icon


my_image1=ImageTk.PhotoImage(Image.open('unsplash1.jpg')) #add photo
my_image2=ImageTk.PhotoImage(Image.open('unsplash2.jpg'))
my_image3=ImageTk.PhotoImage(Image.open('unsplash3.jpg'))
my_image4=ImageTk.PhotoImage(Image.open('unsplash4.jpg'))
my_image5=ImageTk.PhotoImage(Image.open('unsplash5.jpg'))

image_list=[my_image1,my_image2,my_image3,my_image4,my_image5]

my_label=Label(image=my_image1)
my_label.grid(row=0,column=0,columnspan=3)

def function_forward_button(image_number):


   global my_label
   global forward_button
   global back_button

   my_label.grid_forget()
   my_label=Label(image=image_list[image_number-1])

   forward_button =Button(root,text=">>",command=lambda x = image_number+1:function_forward_button(x))

   back_button=Button(root,text="<<",command=lambda:back_button(image_number-1))

   if image_number == 5:
       forward_button=Button(root, text=">>", state=DISABLED)

   my_label.grid(row=0,column=0,columnspan=3)
   back_button.grid(row=1,column=0)
   forward_button.grid(row=1, column=2)

def back_button():
   global my_label
   global forward_button
   global back_button


back_button=Button(root,text="<<",command=back_button)
exit_button=Button(root,text="Exit Program",command=root.quit)
forward_button=Button(root,text=">>",command=lambda x=2:function_forward_button(x)) #ERROR


back_button.grid(row=1,column=0)
exit_button.grid(row=1,column=1)
forward_button.grid(row=1, column=2)



root.mainloop()

Upvotes: 0

TheLizzard
TheLizzard

Reputation: 7680

Instead of recreating the buttons you can just configure the old ones like this:

#menambah icon gambar tombol exit, dll
import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()
root.title("Study Tkinter")

root.iconbitmap("inst.png") #add icon


image_list = [ImageTk.PhotoImage(file=f"unsplash{i}.jpg") for i in range(1, 6)]
current_image_shown = 0

my_label = tk.Label(root, image=image_list[current_image_shown])
my_label.grid(row=0, column=0, columnspan=3)

def forward_button_pressed():
    global current_image_shown
    current_image_shown += 1

    my_label.config(image=image_list[current_image_shown])

    if current_image_shown == 5:
        forward_button.config(state="disabled")
    else:
        forward_button.config(state="normal")


def back_button_pressed():
    global current_image_shown
    current_image_shown -= 1

    my_label.config(image=image_list[current_image_shown])

    if current_image_shown == 0:
        back_button.config(state="disabled")
    else:
        back_button.config(state="normal")


back_button = tk.Button(root, text="<<", command=back_button_pressed, state="disabled")
exit_button = tk.Button(root, text="Exit", command=root.destroy)
forward_button = tk.Button(root, text=">>", command=forward_button_pressed)

back_button.grid(row=1,column=0)
exit_button.grid(row=1,column=1)
forward_button.grid(row=1, column=2)

root.mainloop()

Also I am using a global variable named current_image_shown. It stores the index of the currently shown image from image_list.

Upvotes: 2

Related Questions