Python 单击tkinter按钮时更改图片
我正在使用Python3中的tkinter生成一些按钮和标签,这些按钮和标签在单击按钮时会改变颜色(并在Rasp pi上打开GPIO引脚) 是否可以更改单击按钮时按钮使用的.gif?我希望它在GPIO引脚关闭时显示ON,在GPIO引脚打开时显示off 目前我有:Python 单击tkinter按钮时更改图片,python,tkinter,raspberry-pi3,Python,Tkinter,Raspberry Pi3,我正在使用Python3中的tkinter生成一些按钮和标签,这些按钮和标签在单击按钮时会改变颜色(并在Rasp pi上打开GPIO引脚) 是否可以更改单击按钮时按钮使用的.gif?我希望它在GPIO引脚关闭时显示ON,在GPIO引脚打开时显示off 目前我有: #BCM17 GPIO.setup(17,GPIO.OUT) colour17=StringVar() pinstate17=GPIO.input(17) if pinstate17==1: colour17.set('red'
#BCM17
GPIO.setup(17,GPIO.OUT)
colour17=StringVar()
pinstate17=GPIO.input(17)
if pinstate17==1:
colour17.set('red')
else:
colour17.set('green')
BCM17Bimage=tk.PhotoImage(file='on.gif')
BCM17B = Button(clock, text="GPIO 0\nBCM 17", image=BCM17Bimage, width=78, height=100, bg="grey", command=BCM17f).grid(column=2, row=1)
BCM17L = Label(clock, text="GPIO 0\nBCM 17", font=(fontname,12), fg='white', bg=colour17.get(), width=10, height=2)
BCM17L.grid(column=0, row=1)
并且,按钮的def为:
def BCM17f():
pinstate17=GPIO.input(17)
colour17.set('red' if pinstate17==0 else 'green')
BCM17L.configure(bg=colour17.get())
if pinstate17==0:
GPIO.output(17,True)
else:
GPIO.output(17,False)
print(pinstate17)
一个随机的旁白也-有可能得到一封电子邮件时,人们回复一个帖子在这里?外观很好,但在任何地方都看不到它的选项。解决了它:
#BCM17
GPIO.setup(17,GPIO.OUT)
colour17=StringVar()
pinstate17=GPIO.input(17)
if pinstate17==1:
colour17.set('red')
else:
colour17.set('green')
BCM17L = Label(clock, text="GPIO 0\nBCM 17", font=(fontname,12), fg='white', bg=colour17.get(), width=10, height=2)
BCM17L.grid(column=0, row=1)
image17on=tk.PhotoImage(file="on.gif")
image17off=tk.PhotoImage(file="off.gif")
if pinstate17==1:
image17=image17on
else:
image17=image17off
BCM17B = Button(clock, text="GPIO 0\nBCM 17",
image=image17,
width=75, height=75, bg="grey",
command=BCM17f)
BCM17B.grid(column=2, row=1)
对于def:
def BCM17f():
pinstate17=GPIO.input(17)
colour17.set('red' if pinstate17==0 else 'green')
BCM17L.configure(bg=colour17.get())
global toggle17
if toggle17 and pinstate17==1:
GPIO.output(17,False)
BCM17B.config(image=image17off)
toggle17 = not toggle17
else:
GPIO.output(17,True)
BCM17B.config(image=image17on)
toggle17 = not toggle17
问这个问题之前,你搜索过这个网站吗?在这个网站上,有很多关于更改小部件上的图像的问题。是的,我找不到任何与此相关的具体问题。