【发布时间】:2016-08-16 11:00:50
【问题描述】:
我在 python 3 中使用 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')
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 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)
也是随机的——当人们回复这里的帖子时,是否有可能收到一封电子邮件?看起来不错,但在任何地方都看不到它的选项。
【问题讨论】:
-
你在问这个问题之前搜索过这个网站吗?这个网站上有很多与更改小部件上的图像有关的问题。
-
是的,我找不到任何专门提到这个的东西。
标签: python tkinter raspberry-pi3