I am trying to make an application where if I left click a button in a grid it changes the images to 1.jpg and when it is right clicked it changes to 2.jpg. I tried it several times but the image is not changing on right click.
This is how the grid looks like: image
The code:
from tkinter import *
from PIL import ImageTk, Image
WIN = Tk()
WIN.geometry("280x660+300+10")
Button_Frame = Frame(WIN,width=399,height=100)
Button_Frame.pack()
Main = Frame(WIN,width=399,height=399)
Main.pack()
img1 = Image.open("2.jpg")
img1 = img1.resize((57, 57), Image.ANTIALIAS)
photo1 = ImageTk.PhotoImage(img1)
img2 = Image.open("3.jpg")
img2 = img2.resize((57, 57), Image.ANTIALIAS)
photo2 = ImageTk.PhotoImage(img2)
button_list = []
type_click = ""
for i in range(49):
f1 = Frame(Main,width=40,height=40,bg="black")
b = Button(f1, text = "",borderwidth=4,bg="white")
button_list.append(b)
def first(k):
if type_click == "left":
button_list[k].configure(image=photo1)
elif type_click == "right":
button_list[k].configure(image=photo2)
def left(event):
global type_click
type_click = "left"
print("left")
def right(event):
global type_click
type_click = "right"
print("right")
b.configure(command=lambda k=i:first(k))
b.bind("<Button-1>",left)
b.bind("<Button-3>",right)
f1.rowconfigure(0, weight = 1)
f1.columnconfigure(0, weight = 1)
f1.grid_propagate(0)
f1.grid(row = i//7, column = i%7)
# b.bind("<Button-1>",lambda k=i:first(k))
b.grid(sticky = "NWSE")
WIN.mainloop()
Please tell me where am I going wrong