Hallo Zusammen,
mit einem Pi und einem Touch-Display habe ich ein Webradio gebaut. Als Feierabendbeschäftigung bin ich nun dabei mittels Python dem Gerät "Leben" einzuhauchen.
Das ging auch bisher ganz gut. Allerdings bin ich jetzt an einem Punkt angekommen, wo ich nicht mehr weiter komme. Ich möchte die Darstellung der Button in Abhängigkeit von Zustand auf dem Display ändern. z.B. letzter Sender errreicht -> Button Sender plus wird grau. Ich habe schon einiges probiert, aber es gelingt mir einfach nicht. Wieso kann ich die mit "lambda:steuerung(00)" übergebenen Wert nicht weiter im Programm auswerten?
Den Code habe ich unten angefügt.
Python
#!/usr/bin/python3.2
from tkinter import *
import os
import subprocess
from time import *
import _thread
import os.path
import sqlite3
from datetime import date
hgrd = "#071a52"
sfrb = "#a1ebf0"
###__RADIOSTEUERUNG__##################
def steuerung(bn):
if bn == 00: #Radio
os.system("mpc clear")
os.system("mpc load sender")
os.system("mpc play")
os.system("mpc volume 5")
if bn == 10: #USB
os.system("mpc clear")
os.system("mpc play")
os.system("mpc volume 5")
elif bn == 20: #NAS
os.system("mpc clear")
os.system("mpc play")
os.system("mpc volume 5")
elif bn == 30:
os.system("mpc stop")
os.system("mpc clear")
os._exit(1)
elif bn == 40: #Aus
os.system("sudo shutdown -h now")
elif bn == 41: #Start
os.system("mpc play")
elif bn == 42: #Stop
os.system("mpc stop")
elif bn == 43: #Rückwärts
os.system("mpc prev")
elif bn == 44: #Vorwärts
os.system("mpc next")
elif bn == 45: #Sender zurück
os.system("mpc prev")
elif bn == 46: #Sender vor
os.system("mpc next")
elif bn == 47: #Ton aus
os.system ("mpc volume 0")
elif bn== 37: #Leiser
os.system("mpc volume -5")
elif bn== 17: #Lauter
os.system("mpc volume +5")
month=["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
def checkTime():
while True:
t = localtime()
heute = date.today()
wtag = ("Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag","Sonntag")[heute.weekday()]
if (t.tm_sec) <= 9:
sec = "0" + str(t.tm_sec)
else:
sec = str(t.tm_sec)
if (t.tm_min) <=9:
min = "0" + str(t.tm_min)
else:
min = str(t.tm_min)
if (t.tm_hour) <=9:
hour = "0" + str(t.tm_hour)
else:
hour = str(t.tm_hour)
if (t.tm_mday)<=9:
tag = "0" + str(t.tm_mday)
else:
tag = str(t.tm_mday)
timeText = "Webradio" + "\n" + wtag + " der " + tag + ". "
timeText += month[t.tm_mon - 1] + " "
timeText += str(t.tm_year)+" " + hour+":" + min + ":" + sec + "Uhr"
Uhr.config(text=timeText)
sleep(1)
def checkAnzeige():
while True:
mpcname =subprocess.Popen(['mpc' , 'current' , '-f' , '%name%'], stdout =subprocess.PIPE).communicate() [0].decode('utf-8').strip()
mpctitle =subprocess.Popen(['mpc' , 'current' , '-f' , '%title%'], stdout =subprocess.PIPE).communicate() [0].decode('utf-8').strip()
if len(mpctitle) <= 46:
mpcinfo = mpcname + "\n" + mpctitle
else:
tlg = len(mpctitle)
tpo = mpctitle.find(" ",30)
mpcinfo = mpcname + "\n" + mpctitle[:tpo] + "\n" + mpctitle[tpo+1:]
ausgabe = mpcinfo
if not os.path.isfile("/home/pi/webradio/sendericon/"+mpcname+".gif"):
sendericon = PhotoImage(file="/home/pi/webradio/sendericon/radiodefault.gif")
else:
sendericon = PhotoImage(file="/home/pi/webradio/sendericon/"+mpcname+".gif")
logo.config(image=sendericon)
sender.config(text=ausgabe)
sleep(1)
####__DISPLAY-LAYOUT__##################
##_Hauptfenster festlegen
main=Tk()
main.title('WEBRADIO')
main.overrideredirect(True)
main.geometry("800x500+0+0".format(main.winfo_screenwidth(),main.winfo_screenheight()))
main.focus_set()
main.configure(bg=hgrd)
##_Buttons festlegen
#_Icon's für Button festlegen
ph00= PhotoImage(file="/home/pi/webradio/buttonicon/radio.gif")
ph10= PhotoImage(file="/home/pi/webradio/buttonicon/usb.gif")
ph20= PhotoImage(file="/home/pi/webradio/buttonicon/nas.gif")
ph30= PhotoImage(file="/home/pi/webradio/buttonicon/config.gif")
ph40= PhotoImage(file="/home/pi/webradio/buttonicon/aus.gif")
ph41= PhotoImage(file="/home/pi/webradio/buttonicon/play.gif")
ph42= PhotoImage(file="/home/pi/webradio/buttonicon/stop.gif")
ph43= PhotoImage(file="/home/pi/webradio/buttonicon/prev.gif")
ph44= PhotoImage(file="/home/pi/webradio/buttonicon/next.gif")
ph45= PhotoImage(file="/home/pi/webradio/buttonicon/sendm.gif")
ph46= PhotoImage(file="/home/pi/webradio/buttonicon/sendp.gif")
ph47= PhotoImage(file="/home/pi/webradio/buttonicon/mute.gif")
ph37= PhotoImage(file="/home/pi/webradio/buttonicon/volm.gif")
ph17= PhotoImage(file="/home/pi/webradio/buttonicon/volp.gif")
#_Buttons und Labes Eigenschaften festlegen
bn00=Button(main,command =lambda:steuerung(00),image=ph00,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn10=Button(main,command =lambda:steuerung(10),image=ph10,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn17=Button(main,command =lambda:steuerung(17),image=ph17,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn20=Button(main,command =lambda:steuerung(20),image=ph20,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn30=Button(main,command =lambda:steuerung(30),image=ph30,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn37=Button(main,command =lambda:steuerung(37),image=ph37,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn40=Button(main,command =lambda:steuerung(40),image=ph40,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn41=Button(main,command =lambda:steuerung(41),image=ph41,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn42=Button(main,command =lambda:steuerung(42),image=ph42,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn43=Button(main,command =lambda:steuerung(43),image=ph43,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn44=Button(main,command =lambda:steuerung(44),image=ph44,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn45=Button(main,command =lambda:steuerung(45),image=ph45,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn46=Button(main,command =lambda:steuerung(46),image=ph46,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
bn47=Button(main,command =lambda:steuerung(47),image=ph47,relief='flat',activebackground=hgrd,bg=hgrd,width=85,height=85)
Uhr=Label(main, font=('calibri',16,'bold'),fg=sfrb,bg=hgrd)
logo=Label(main,width=175,height=175)
tinfo=Label(main,font=('calibri',16,'bold'),fg=sfrb,bg=hgrd)
sender=Label(main,bg=hgrd,font=('calibri',16),fg=sfrb,height=3)
#_Buttons und Labels platzieren
bn00.grid(row=0,column=0,padx=1,pady=1,sticky=S)
bn10.grid(row=1,column=0,padx=1,pady=1)
bn17.grid(row=1,column=7,padx=10,pady=1)
bn20.grid(row=2,column=0,padx=1,pady=1)
bn30.grid(row=3,column=0,padx=1,pady=1)
bn37.grid(row=3,column=7,padx=10,pady=1)
bn40.grid(row=4,column=0,padx=10,pady=1)
bn41.grid(row=4,column=1,padx=1,pady=1)
bn42.grid(row=4,column=2,padx=1,pady=1)
bn43.grid(row=4,column=3,padx=1,pady=1)
bn44.grid(row=4,column=4,padx=1,pady=1)
bn45.grid(row=4,column=5,padx=1,pady=1)
bn46.grid(row=4,column=6,padx=1,pady=1)
bn47.grid(row=4,column=7,padx=10,pady=1)
Uhr.grid(row=0,column=1,columnspan=6,sticky=W+E)
logo.grid(row=1,column=1,rowspan=2,columnspan=2,padx=1,pady=1)
tinfo.grid(row=1,rowspan=2,columnspan=4,column=3,padx=1,pady=1)
sender.grid(row=3,column=1,columnspan=6,padx=1,pady=1,sticky=W+E)
_thread.start_new_thread(checkTime, ())
_thread.start_new_thread(checkAnzeige, ())
# Programmschleife
main.mainloop()
Alles anzeigen
Vielen Dank für Eure Hilfe im Voraus!
voi