Python 3.x tkinter键绑定导致图像消失

Python 3.x tkinter键绑定导致图像消失,python-3.x,tkinter,Python 3.x,Tkinter,我正在尝试将箭头键绑定到按钮,或者至少绑定按钮的功能(按钮向前和按钮向后)。按钮的功能是有效的,但是,当我将一个键绑定到按钮时,图像就消失了 如果有人能帮我弄清楚如何创建一个循环来定义图像并将其放入列表中,那也太棒了。说到那件事,我真是不知所措 该代码的主要目的是作为一个图像查看器,当图像发生变化时,它会使LED条闪烁。 我想能够控制它使用箭头键之间的图像前进和后退 from tkinter import Tk, Button,Label, DISABLED from PIL import Im

我正在尝试将箭头键绑定到按钮,或者至少绑定按钮的功能(按钮向前和按钮向后)。按钮的功能是有效的,但是,当我将一个键绑定到按钮时,图像就消失了

如果有人能帮我弄清楚如何创建一个循环来定义图像并将其放入列表中,那也太棒了。说到那件事,我真是不知所措

该代码的主要目的是作为一个图像查看器,当图像发生变化时,它会使LED条闪烁。 我想能够控制它使用箭头键之间的图像前进和后退

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()
从tkinter导入Tk,按钮,标签,禁用
从PIL导入ImageTk,图像
进口局
导入时间
进口Neopix
像素=新像素。新像素(board.D18,30)
root=Tk()
root.configure(bg='black')
root.title(“请工作”)
#定义、加载、显示
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”))
图像列表=[我的img1,我的img2,我的img3,我的img4,我的img5,我的img6,我的img7,我的img8]
我的标签=标签(图像=我的img1)
my_label.grid(行=0,列=0,列span=3,行span=25,padx=440,pady=5)
def转发(图像编号,事件=无):
全球my_标签
全局按钮\u前进
全局按钮返回
我的标签。网格忘记()
我的标签=标签(图像=图像列表[图像编号-1])
按钮向前=按钮(根,text=“next”,命令=lambda:forward(图像编号+1))
按钮返回=按钮(根,text=“previous”,命令=lambda:back(图像编号1))
如果图像_编号==7:
按钮\前进=按钮(根,text=“下一步”,状态=禁用)
my_label.grid(行=0,列=0,列span=3,行span=25,padx=440,pady=5)
按钮返回网格(行=23,列=0)
按钮向前网格(行=23,列=2)
像素。填充((255,0,0))
睡眠时间(0.1)
像素。填充((0,0,0))
睡眠时间(0.5)
def back(图像_编号,):
全球my_标签
全局按钮\u前进
全局按钮返回
我的标签。网格忘记()
我的标签=标签(图像=图像列表[图像编号-1])
按钮向前=按钮(根,text=“next”,命令=lambda:forward(图像编号+1))
按钮返回=按钮(根,text=“previous”,命令=lambda:back(图像编号1))
my_label.grid(行=0,列=0,列span=3,行span=25,padx=440,pady=5)
如果图像_编号==1:
按钮返回=按钮(root,text=“previous”,state=已禁用)
按钮返回网格(行=23,列=0)
按钮退出网格(行=23,列=1)
按钮向前网格(行=23,列=2)
像素。填充((255,0,0))
睡眠时间(0.1)
像素。填充((0,0,0))
睡眠时间(0.5)
按钮返回=按钮(root,text=“previous”,command=back)
按钮退出=按钮(root,text=“exit”,command=root.quit)
按钮\前进=按钮(root,text=“next”,command=lambda:forward(2))
root.bind(“”,后面)
root.bind(“”,向前)
按钮返回网格(行=23,列=0)
按钮退出网格(行=23,列=1)
按钮向前网格(行=23,列=2)
root.mainloop()

您的绑定无法工作,因为将传递与按键对应的
事件,而不是
back()
forward()
中的
image\u number
参数

此外,您每次都在重新创建小部件,而您只需重新配置它们。因此,要更改标签的图像,可以执行以下操作:
my\u label.configure(image=)
。另外,我宁愿使用全局变量,而不是将
image\u number
作为参数传递,以便更容易使用键绑定中的函数:

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()
从tkinter导入Tk,按钮,标签,禁用,正常
从PIL导入ImageTk,图像
进口局
导入时间
进口Neopix
像素=新像素。新像素(board.D18,30)
root=Tk()
root.configure(bg='black')
root.title(“请工作”)
#定义、加载、显示
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”))
图像列表=[我的img1,我的img2,我的img3,我的img4,我的img5,我的img6,我的img7,我的img8]
图像编号=1
#只创建一次标签
我的标签=标签(根,图像=图像列表[图像编号-1])
my_label.grid(行=0,列=0,列span=3,行span=25,padx=440,pady=5)
def forward(事件=无):
全局图像_编号#用于跟踪显示图像的全局变量
图像编号+=1
#更改标签中的图像
my_label.configure(image=image\u list[image\u number-1])
如果图像_编号==8:
#最后一个图像,禁用前进按钮
按钮向前。配置(状态=禁用)
elif图像_编号==2:
#不再是第一个图像,请重新启用“后退”按钮
按钮返回。配置(状态=正常)
像素。填充((255,0,0))
睡眠时间(0.1)
像素。填充((0,0,0))
睡眠时间(0.5)
def返回(事件=无):
全局映像编号
图像编号-=1
my_label.configure(image=image\u list[image\u number-1])
如果图像_编号==1:
#第一个图像,禁用后退按钮
按钮返回。配置(状态=禁用)
elif图像_编号==7:
#不再是最后一张图像,请重新启用“前进”按钮
按钮向前。配置(状态=正常)
像素。填充((255,0,0))
睡眠时间(0.1)
像素。填充((0,0,0))
睡眠时间(0.5)
按钮返回=按钮(根,t