"button_back = Button(root, text = "<<", command = lambda: back(image_number-1)) TypeError: back() takes 0 positional arguments but 1 was given"

Viewed 47

I want to make a little app with Tkinter and it's about a photo viewer

There are 3 buttons on my Interface[Back , Exit , Forward], and the Exit button and the Forward button work good but unfortunately, I get some errors with the Back button

Forward Button: Move forward for the next photo Exit Button: Quit the app Back Button: Go back to the previous photo

Error is:

"button_back = Button(root, text = "<<", command = lambda: back(image_number-1)) TypeError: back() takes 0 positional arguments but 1 was given"

Code:

from Tkinter import *
from PIL import ImageTk, Image

my_img1 = ImageTk.PhotoImage(Image.open("one.png")) #-First Step
my_img2 = ImageTk.PhotoImage(Image.open("two.png")) 
my_img3 = ImageTk.PhotoImage(Image.open("three.png")) 
my_img4 = ImageTk.PhotoImage(Image.open("four.png")) 

image_list = [my_img1, my_img2, my_img3, my_img4]

my_label = Label(image=my_img1)     #Three Button
my_label.grid(row =0, column = 0, columnspan=3)

def back():

    global my_label
    global button_forward
    global button_back

def forward(image_number):

    global my_label
    global button_forward
    global button_back
    
    my_label.grid_forget()
    my_label = Label(image=image_list[image_number-1])
    
    button_forward = Button(root, text = ">>", command = 
               lambda:forward(image_number+1))
        #ERROR
    button_back = Button(root, text = "<<", command = lambda: back(image_number-1))
    
    my_label.grid(row =0, column = 0, columnspan=3)
    button_back.grid(row= 1, column=0)
    button_forward.grid(row = 1, column = 2)
    
    


button_back = Button(root, text = "<<", command = back)
button_forward = Button(root, text = ">>", command = lambda : forward(2))
button_exit = Button(root, text = "EXIT PROGRAM", command = root.quit)


button_back.grid(row= 1, column=0)
button_exit.grid(row = 1, column = 1)
button_forward.grid(row = 1, column = 2)

root.mainloop()
1 Answers

You forgot image_number in def back(image_number):.
And it will need also command=lambda:back(0) at start.


But frankly it could be simpler to use global variable current_image and use this variable directly in functions.

It wouldn't need to create again buttons (or assign new function to buttons like button_forward["command"] = lambda:forward(image_number+1))

And you don't have to create again label but you can replace image in existing label - my_label["image"] = ...


It could be something like this:

import tkinter as tk  # PEP8: `import *` is not preferred
from PIL import ImageTk  # no need `Image` if you don't change images - ie. resize, crop, convert to grayscale
#import glob

# --- functions ---  # PEP8: all functions before main code

def back():
    global current_image

    if current_image > 0:
        current_image -= 1
    #else:    # looping images
    #    current_image = len(image_list)-1

    my_label["image"] = image_list[current_image]

def forward():
    global current_image
    
    if current_image < len(image_list)-1:
        current_image += 1
    #else:    # looping images
    #    current_image = 0
        
    my_label["image"] = image_list[current_image]

# --- main ---

filenames = ["one.png", "two.png", "three.png", "four.png"]
#filenames = glob.glob("*.png") + glob.glob("*.jpg")

# ---

root = tk.Tk()

image_list = []

for name in filenames:
    img = ImageTk.PhotoImage(file=name)  # without `Image()` it has to use `file=`
    image_list.append(img)

current_image = 0

# ---

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

button_back = tk.Button(root, text="<<", command=back)  # PEP8: inside `()` without spaces aroung `=`
button_forward = tk.Button(root, text=">>", command=forward)
button_exit = tk.Button(root, text="EXIT PROGRAM", command=root.destroy)  # `.destroy` is better then `.quit`

button_back.grid(row=1, column=0)
button_exit.grid(row=1, column=1)
button_forward.grid(row=1, column=2)

root.mainloop()

PEP 8 -- Style Guide for Python Code

Related