2016-08-16 43 views
-2

我使用的Tkinter在Python 3产生一些按钮和一些标签按钮被点击时改变颜色(和锉刀PI打开一个GPIO引脚太)更改图片时Tkinter的按钮,点击

是有可能改变点击按钮时按钮使用的.gif文件?当GPIO引脚关闭时,我希望它打开,当GPIO引脚打开时,我希望它打开。

目前我有:

#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) 

随机预留太多 - 这可能当人们回复到收到一封电子邮件张贴在这里?有一个好看,但无法在任何地方看到一个选项。

+0

你有没有问这个问题之前搜索到的网站?这个网站上有很多与更改小部件上的图像有关的问题。 –

+0

是的,我不能找到任何特别提到这一点。 – Jon

回答

0

解决它:

#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