Estoy tratando de vincular las teclas de flecha a los botones o al menos a las funciones de los botones (button_forward y button_back). La función de los botones funciona, sin embargo, cuando ato una tecla al botón, la imagen simplemente desaparece.

También sería genial si alguien pudiera ayudarme a descubrir cómo crear un bucle para definir las imágenes y ponerlas en una lista. Estoy tan perdido cuando se trata de eso.

El propósito principal del código es ser un visor de imágenes que hace parpadear una tira de LED cuando cambia una imagen. Quiero poder controlarlo usando las teclas de flecha para avanzar y retroceder entre las imágenes.

from tkinter import Tk, Button,Label, DISABLED
from PIL import ImageTk,Image
import board
import time
import neopixel
pixels = neopixel.NeoPixel(board.D18, 30)

root= Tk()
root.configure(bg='black')
root.title("please work")

# define, load, show
my_img1 = ImageTk.PhotoImage(Image.open("1.bmp"))
my_img2 = ImageTk.PhotoImage(Image.open("2.bmp"))
my_img3 = ImageTk.PhotoImage(Image.open("3.bmp"))
my_img4 = ImageTk.PhotoImage(Image.open("4.bmp"))
my_img5 = ImageTk.PhotoImage(Image.open("5.bmp"))
my_img6 = ImageTk.PhotoImage(Image.open("6.bmp"))
my_img7 = ImageTk.PhotoImage(Image.open("7.bmp"))
my_img8 = ImageTk.PhotoImage(Image.open("8.bmp"))


image_list = [my_img1, my_img2, my_img3, my_img4, my_img5, my_img6, my_img7, my_img8]

my_label = Label(image=my_img1)
my_label.grid(row = 0, column = 0, columnspan= 3, rowspan = 25, padx=440, pady= 5)


def forward(image_number, event = None):
    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 = "next", command=lambda: forward(image_number+1))
    button_back = Button(root, text = "previous", command = lambda: back(image_number-1))
   
    
    if image_number == 7:
        button_forward = Button(root, text = "next", state = DISABLED)
        
    my_label.grid(row = 0, column = 0, columnspan = 3, rowspan = 25, padx=440, pady= 5) 
    button_back.grid(row = 23, column = 0)
    button_forward.grid(row = 23, column = 2)
    
    pixels.fill((255,0,0))
    time.sleep(0.1)
    pixels.fill((0,0,0))
    time.sleep(0.5)

    
def back(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 = "next", command=lambda: forward(image_number+1))
    button_back = Button(root, text = "previous", command = lambda: back(image_number-1))
    my_label.grid(row = 0, column = 0, columnspan = 3, rowspan = 25, padx=440, pady= 5)
        
    if image_number == 1:
            button_back = Button(root, text = "previous", state = DISABLED)
    
   
    button_back.grid(row=23, column = 0 )
    button_exit.grid(row=23, column = 1 )
    button_forward.grid(row=23, column = 2)
    
    
    pixels.fill((255,0,0))
    time.sleep(0.1)
    pixels.fill((0,0,0))
    time.sleep(0.5)

    
button_back = Button(root, text = "previous", command = back)
button_exit = Button(root, text = "Exit", command = root.quit)
button_forward = Button(root, text = "next", command =lambda:forward(2))

root.bind('<Left>', back)
root.bind('<Right>', forward)

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

root.mainloop()
0
nik2182 1 sep. 2020 a las 07:50

1 respuesta

La mejor respuesta

Sus enlaces no pueden funcionar porque el event correspondiente a la pulsación de tecla se pasará en lugar del argumento image_number en back() y forward().

Además, está recreando los widgets cada vez, mientras que solo debe reconfigurarlos. Entonces, para cambiar la imagen de la etiqueta, puede hacer: my_label.configure(image=<new image>). Además, en lugar de pasar image_number como argumento, prefiero usar una variable global, para que sea más fácil usar las funciones en las combinaciones de teclas:

from tkinter import Tk, Button, Label, DISABLED, NORMAL
from PIL import ImageTk, Image
import board
import time
import neopixel

pixels = neopixel.NeoPixel(board.D18, 30)

root = Tk()
root.configure(bg='black')
root.title("please work")

# define, load, show
my_img1 = ImageTk.PhotoImage(Image.open("1.bmp"))
my_img2 = ImageTk.PhotoImage(Image.open("2.bmp"))
my_img3 = ImageTk.PhotoImage(Image.open("3.bmp"))
my_img4 = ImageTk.PhotoImage(Image.open("4.bmp"))
my_img5 = ImageTk.PhotoImage(Image.open("5.bmp"))
my_img6 = ImageTk.PhotoImage(Image.open("6.bmp"))
my_img7 = ImageTk.PhotoImage(Image.open("7.bmp"))
my_img8 = ImageTk.PhotoImage(Image.open("8.bmp"))

image_list = [my_img1, my_img2, my_img3, my_img4, my_img5, my_img6, my_img7, my_img8]

image_number = 1 
# create the label only once
my_label = Label(root, image=image_list[image_number - 1])
my_label.grid(row=0, column=0, columnspan= 3, rowspan=25, padx=440, pady= 5)


def forward(event=None):
    global image_number   # global variable to keep track of the displayed image
    image_number += 1
    # change image in label
    my_label.configure(image=image_list[image_number - 1])
    if image_number == 8:
        # last image, disable forward button
        button_forward.configure(state=DISABLED)
    elif image_number == 2:
        # no longer first image, re-enable back button
        button_back.configure(state=NORMAL)

    pixels.fill((255,0,0))
    time.sleep(0.1)
    pixels.fill((0,0,0))
    time.sleep(0.5)


def back(event=None):
    global image_number
    image_number -= 1
    my_label.configure(image=image_list[image_number - 1])

    if image_number == 1:
        # first image, disable back button
        button_back.configure(state=DISABLED)
    elif image_number == 7:
        # no longer last image, re-enable forward button
        button_forward.configure(state=NORMAL)

    pixels.fill((255,0,0))
    time.sleep(0.1)
    pixels.fill((0,0,0))
    time.sleep(0.5)


button_back = Button(root, text="previous", command=back, state=DISABLED)
button_exit = Button(root, text="Exit", command=root.quit)
button_forward = Button(root, text="next", command=forward)

root.bind('<Left>', back)
root.bind('<Right>', forward)

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

root.mainloop()
0
j_4321 1 sep. 2020 a las 11:41