当我 gpio 状态改变时,标签内的 tkinter 图像改变
tkinter image change within a label when i gpio state changes
我正在尝试使用标签内的图像来显示 gpio 状态的变化,我已经能够使用文本进行更改,但现在想使用 LED 的图片和鱼的图像。但我找不到它。
import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#Setup input GPIO pins for use with optical infrared sensors
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP LOW WATER LEVEL SENSOR
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP NORM WATER LEVEL SENSOR
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP HIGH WATER LEVEL SENSOR
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP) # RO LOW WATER LEVEL SENSOR
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY HIGH WATER LEVEL
#GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # MAY USE AS PROTIEN SKIMMER SENSOR
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY LOW WATER LEVEL SENSOR
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT) #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT) #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT) #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT) #used for return pump 1
GPIO.setup(9, GPIO.OUT) #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT) #power head 1 ch
GPIO.setup(7, GPIO.OUT) # AUTO TOP OFF PUMP
#Setup output GPIO pins for use with Red Yellow and Green LEDS
GPIO.setup(23, GPIO.OUT) # DISPLAY HIGH WATER LEVEL RED LED
GPIO.setup(24, GPIO.OUT) # DISPLAY NORM WATER LEVEL GREEN LED
GPIO.setup(25, GPIO.OUT) # DISPLAY LOW WATER LEVEL YELLOW LED
GPIO.setup(12, GPIO.OUT) # AUTO TOP OFF LOW LEVEL RED LED
GPIO.setup(16, GPIO.OUT) # SUMP HIGH WATER LEVEL RED LED
GPIO.setup(20, GPIO.OUT) # SUMP NORMAL WATER LEVEL GREEN LED
GPIO.setup(21, GPIO.OUT) # SUMP LOW WATER LEVEL YELLOW LED
#setup fonts
ftb= 'Verdana', 12, 'bold'
def photo2():
photo2= oraled.png
#setup class for the tk program
class gpio(tk.Tk):
def __init__(root, *args, **kwargs):
tk.Tk.__init__(root, *args, **kwargs)
root.title("trial")
root.geometry("430x830")
root.configure(bg="lightblue")
#setup image
root.photo1 = tk.PhotoImage(file="fish.gif") #defines a photo and gives the file name
root.label1 = tk.Label(root, image=root.photo1)#puts label in the window in this case not text file must be in program folder
root.label1.place(x=0, y=0) #says how to place the label
#setup display high sensor label
root.photo2 = tk.PhotoImage(file='oraled.png')
root.dishisensorlabel= tk.Label(root, image="")
root.dishisensorlabel.place(x=210, y=500)
labeldishisensor= tk.Label(root,text=("Display High"), font=(ftb), fg="black")
labeldishisensor.place(x=0, y=500)
root.update_gpio()
def update_gpio(root):
dhsen =('fish.gif' if GPIO.input(5) else 'oraled')
root.dishisensorlabel.configure(image=dhsen)
root.after(200, root.update_gpio)
if __name__== "__main__":
app = gpio()
app.mainloop()
我收到以下错误 我正在 运行 在 thorney 删除脚本时
>>> %Run 'use of images for leds.py'
Traceback (most recent call last):
File "/home/pi/use of images for leds.py", line 63, in <module>
app = gpio()
File "/home/pi/use of images for leds.py", line 55, in __init__
root.update_gpio()
File "/home/pi/use of images for leds.py", line 58, in update_gpio
root.dishisensorlabel.configure(image=dhsen)
File "/usr/lib/python3.7/tkinter/__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File "/usr/lib/python3.7/tkinter/__init__.py", line 1476, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "fish.gif" doesn't exist
>>>
任何帮助将不胜感激
感谢您表现出兴趣,现在可以使用了,我希望它能帮助其他尝试类似脚本的人
import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#Setup input GPIO pins for use with optical infrared sensors
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP LOW WATER LEVEL SENSOR
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP NORM WATER LEVEL SENSOR
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP HIGH WATER LEVEL SENSOR
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP) # RO LOW WATER LEVEL SENSOR
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY HIGH WATER LEVEL
#GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # MAY USE AS PROTIEN SKIMMER SENSOR
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY LOW WATER LEVEL SENSOR
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT) #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT) #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT) #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT) #used for return pump 1
GPIO.setup(9, GPIO.OUT) #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT) #power head 1 ch
GPIO.setup(7, GPIO.OUT) # AUTO TOP OFF PUMP
#Setup output GPIO pins for use with Red Yellow and Green LEDS
GPIO.setup(23, GPIO.OUT) # DISPLAY HIGH WATER LEVEL RED LED
GPIO.setup(24, GPIO.OUT) # DISPLAY NORM WATER LEVEL GREEN LED
GPIO.setup(25, GPIO.OUT) # DISPLAY LOW WATER LEVEL YELLOW LED
GPIO.setup(12, GPIO.OUT) # AUTO TOP OFF LOW LEVEL RED LED
GPIO.setup(16, GPIO.OUT) # SUMP HIGH WATER LEVEL RED LED
GPIO.setup(20, GPIO.OUT) # SUMP NORMAL WATER LEVEL GREEN LED
GPIO.setup(21, GPIO.OUT) # SUMP LOW WATER LEVEL YELLOW LED
#setup fonts
ftb= 'Verdana', 12, 'bold'
#setup class for the tk program
class gpio(tk.Tk):
def __init__(root, *args, **kwargs):
tk.Tk.__init__(root, *args, **kwargs)
root.title("trial")
root.geometry("430x830")
root.configure(bg="lightblue")
root.photo2= tk.PhotoImage(file="whiteled.png")
root.photo3= tk.PhotoImage(file='green.png')
#setup image
root.photo1 = tk.PhotoImage(file="fish.gif") #defines a photo and gives the file name
root.label1 = tk.Label(root, image=root.photo1)#puts label in the window in this case not text file must be in program folder
root.label1.place(x=0, y=0) #says how to place the label
#setup display high sensor label
root.dishisensorlabel= tk.Label(root, text="")
root.dishisensorlabel.place(x=210, y=500)
labeldishisensor= tk.Label(root,text=("Power Head 2"), font=(ftb), fg="black")
labeldishisensor.place(x=0, y=500)
root.update_gpio()
def update_gpio(root):
dhsen =(root.photo3 if GPIO.input(9) else root.photo2)
root.dishisensorlabel.configure(image=dhsen, text="")
root.after(200, root.update_gpio)
if __name__== "__main__":
app = gpio()
app.mainloop()
需要进行一些更改。
final code
import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#Setup input GPIO pins for use with optical infrared sensors
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP LOW WATER LEVEL SENSOR
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP NORM WATER LEVEL SENSOR
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP HIGH WATER LEVEL SENSOR
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP) # RO LOW WATER LEVEL SENSOR
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY HIGH WATER LEVEL
#GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # MAY USE AS PROTIEN SKIMMER SENSOR
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY LOW WATER LEVEL SENSOR
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT) #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT) #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT) #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT) #used for return pump 1
GPIO.setup(9, GPIO.OUT) #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT) #power head 1 ch
GPIO.setup(7, GPIO.OUT) # AUTO TOP OFF PUMP
#Setup output GPIO pins for use with Red Yellow and Green LEDS
GPIO.setup(23, GPIO.OUT) # DISPLAY HIGH WATER LEVEL RED LED
GPIO.setup(24, GPIO.OUT) # DISPLAY NORM WATER LEVEL GREEN LED
GPIO.setup(25, GPIO.OUT) # DISPLAY LOW WATER LEVEL YELLOW LED
GPIO.setup(12, GPIO.OUT) # AUTO TOP OFF LOW LEVEL RED LED
GPIO.setup(16, GPIO.OUT) # SUMP HIGH WATER LEVEL RED LED
GPIO.setup(20, GPIO.OUT) # SUMP NORMAL WATER LEVEL GREEN LED
GPIO.setup(21, GPIO.OUT) # SUMP LOW WATER LEVEL YELLOW LED
#setup fonts
ftb= 'Verdana', 12, 'bold'
#setup class for the tk program
class gpio(tk.Tk):
def __init__(root, *args, **kwargs):
tk.Tk.__init__(root, *args, **kwargs)
root.title("Heads Up")
root.geometry("430x830")
root.Greenoff= tk.PhotoImage(file="greenoff.png")
root.Greenon= tk.PhotoImage(file="GreenOnworking.png")
root.Yellowoff= tk.PhotoImage(file="yellowoff.png")
root.Yellowon= tk.PhotoImage(file="yellowon.png")
root.Redoff= tk.PhotoImage(file="redoff.png")
root.Redon= tk.PhotoImage(file="redon.png")
root.pumpoffnew= tk.PhotoImage(file="pumpoffnew.png")
root.pumponnew= tk.PhotoImage(file="pumponnew.png")
root.pumpoffr= tk.PhotoImage(file="pumpoffr.png")
root.pumponr= tk.PhotoImage(file="pumponr.png")
root.pwrhdon= tk.PhotoImage(file="pwrhdon.png")
root.pwrhdoff= tk.PhotoImage(file="pwrhdoff.png")
root.gyreon= tk.PhotoImage(file="gyreon.png")
root.gyreoff= tk.PhotoImage(file="gyreoff.png")
root.sensorin= tk.PhotoImage(file="sensorin.png")
root.sensorout= tk.PhotoImage(file="sensorout.png")
root.lowsensorin= tk.PhotoImage(file="lowsensorin.png")
root.lowsensorout= tk.PhotoImage(file="lowsesorout.png")
root.normsensorin= tk.PhotoImage(file="normsensorin.png")
root.normsensorout= tk.PhotoImage(file="normsesorout.png")
root.highsensorin= tk.PhotoImage(file="highsensorin.png")
root.disphighsensorout= tk.PhotoImage(file="disphighsesorout.png")
#setup image
root.photo1 = tk.PhotoImage(file="background.png") #defines a photo and gives the file name
root.label1 = tk.Label(root, image=root.photo1)#puts label in the window in this case not text file must be in program folder
root.label1.place(x=0, y=0) #says how to place the label
#setup exit button
Exitbutton= tk.Button(root, text="Exit", font=(ftb), width=5, bg="red", fg="white", command=root.destroy)
Exitbutton.place(x=20, y=600)
# setup display water high light
root.dishighlabel =tk.Label(root,)
root.dishighlabel.place(x=291, y=91)
# setup display water norm light
root.disnormlabel =tk.Label(root,)
root.disnormlabel.place(x=203, y=91)
# setup display water low light
root.dislowlabel =tk.Label(root,)
root.dislowlabel.place(x=114, y=91)
# setup sump water high light
root.sumphighlabel =tk.Label(root,)
root.sumphighlabel.place(x=218, y=287)
# setup sump water norn light
root.sumpnormlabel=tk.Label(root,)
root.sumpnormlabel.place(x=154, y=287)
root.sumplowlabel=tk.Label(root,)
root.sumplowlabel.place(x=90, y=287)
# setup Ato water low light label
root.atopledsensorlabel=tk.Label(root,)
root.atopledsensorlabel.place(x=337, y=287)
#set up return pump 1 labels
root.rtnp1label = tk.Label(root,)
root.rtnp1label.place(x=45, y=330)
#setup return pump 2 labels
root.rtnp2label = tk.Label(root,)
root.rtnp2label.place(x=250, y=330)
#setup gyre pump labels
root.gyrelabel =tk.Label(root, text="")
root.gyrelabel.place(x=357, y=65)
#setup powerhead 1 labels
root.pwrhd1label = tk.Label(root, text="")
root.pwrhd1label.place(x=43, y=65)
#setup powerhead 2 labels
root.pwrhd2label = tk.Label(root, text="")
root.pwrhd2label.place(x=43, y=95)
#setup ato pump labels
root.atoppumplabel = tk.Label(root, text="")
root.atoppumplabel.place(x=312, y=330)
#setup display high sensor label
root.dishisensorlabel= tk.Label(root, text="")
root.dishisensorlabel.place(x=0, y=30)
#setup display low sensor label
root.dislosensorlabel= tk.Label(root, text="")
root.dislosensorlabel.place(x=0, y=82)
#setup sump high sensor label
root.sumhisensorlabel= tk.Label(root, text="")
root.sumhisensorlabel.place(x=0, y=227)
#setup sump norm sensor label
root.sumnorsensorlabel= tk.Label(root, text="")
root.sumnorsensorlabel.place(x=0, y=279)
#setup sump low sensor label
root.sumlowsensorlabel= tk.Label(root, text="")
root.sumlowsensorlabel.place(x=0, y=332)
#setup ato sensor label
root.atopsensorlabel= tk.Label(root, text="")
root.atopsensorlabel.place(x=399, y=335)
# start the GPIO checks"
root.update_gpio()
def update_gpio(root):
#sensors
dhsen =(root.disphighsensorout if GPIO.input(5) else root.highsensorin)
root.dishisensorlabel.configure(image=dhsen,)
dlsen =(root.lowsensorout if GPIO.input(13) else root.lowsensorin)
root.dislosensorlabel.configure(image=dlsen,)
shsen =(root.disphighsensorout if GPIO.input(22) else root.highsensorin)
root.sumhisensorlabel.configure(image=shsen,)
snsen =(root.normsensorout if GPIO.input(27) else root.normsensorin)
root.sumnorsensorlabel.configure(image=snsen,)
slsen =(root.lowsensorout if GPIO.input(17) else root.lowsensorin)
root.sumlowsensorlabel.configure(image=slsen,)
atosen =(root.sensorout if GPIO.input(26) else root.sensorin)
root.atopsensorlabel.configure(image=atosen,)
#leds
atop = (root.Redon if GPIO.input(12) else root.Greenon)
root.atopledsensorlabel.configure(image=atop,)
sumh = (root.Redon if GPIO.input(16) else root.Redoff)
root.sumphighlabel.configure(image=sumh,)
sumn = (root.Greenon if GPIO.input(20) else root.Greenoff)
root.sumpnormlabel.configure(image=sumn,)
suml = (root.Yellowon if GPIO.input(21) else root.Yellowoff)
root.sumplowlabel.configure(image=suml,)
dish = (root.Redon if GPIO.input(23) else root.Redoff)
root.dishighlabel.configure(image=dish,)
disn = (root.Greenon if GPIO.input(24) else root.Greenoff)
root.disnormlabel.configure(image=disn,)
disl = (root.Yellowon if GPIO.input(25) else root.Yellowoff)
root.dislowlabel.configure(image=disl,)
#pumps
rt1 = (root.pumponnew if GPIO.input(2) else root.pumpoffnew)
root.rtnp1label.configure(image=rt1,)
rt2 = (root.pumponr if GPIO.input(3) else root.pumpoffr)
root.rtnp2label.configure(image=rt2,)
ph2 = (root.pwrhdon if GPIO.input(9) else root.pwrhdoff)
root.pwrhd2label.configure(image=ph2,)
ph1 = (root.pwrhdon if GPIO.input(11) else root.pwrhdoff)
root.pwrhd1label.configure(image=ph1,)
gyr = (root.gyreon if GPIO.input(4) else root.gyreoff)
root.gyrelabel.configure(image=gyr,)
top1 =(root.pumpoffnew if GPIO.input(7) else root.pumponnew)
root.atoppumplabel.configure(image=top1,)
# call this function again in one second
root.after(1000, root.update_gpio)
if __name__== "__main__":
app = gpio()
app.mainloop()
希望这可以帮助其他人尝试做这种事情。
我正在尝试使用标签内的图像来显示 gpio 状态的变化,我已经能够使用文本进行更改,但现在想使用 LED 的图片和鱼的图像。但我找不到它。
import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#Setup input GPIO pins for use with optical infrared sensors
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP LOW WATER LEVEL SENSOR
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP NORM WATER LEVEL SENSOR
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP HIGH WATER LEVEL SENSOR
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP) # RO LOW WATER LEVEL SENSOR
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY HIGH WATER LEVEL
#GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # MAY USE AS PROTIEN SKIMMER SENSOR
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY LOW WATER LEVEL SENSOR
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT) #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT) #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT) #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT) #used for return pump 1
GPIO.setup(9, GPIO.OUT) #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT) #power head 1 ch
GPIO.setup(7, GPIO.OUT) # AUTO TOP OFF PUMP
#Setup output GPIO pins for use with Red Yellow and Green LEDS
GPIO.setup(23, GPIO.OUT) # DISPLAY HIGH WATER LEVEL RED LED
GPIO.setup(24, GPIO.OUT) # DISPLAY NORM WATER LEVEL GREEN LED
GPIO.setup(25, GPIO.OUT) # DISPLAY LOW WATER LEVEL YELLOW LED
GPIO.setup(12, GPIO.OUT) # AUTO TOP OFF LOW LEVEL RED LED
GPIO.setup(16, GPIO.OUT) # SUMP HIGH WATER LEVEL RED LED
GPIO.setup(20, GPIO.OUT) # SUMP NORMAL WATER LEVEL GREEN LED
GPIO.setup(21, GPIO.OUT) # SUMP LOW WATER LEVEL YELLOW LED
#setup fonts
ftb= 'Verdana', 12, 'bold'
def photo2():
photo2= oraled.png
#setup class for the tk program
class gpio(tk.Tk):
def __init__(root, *args, **kwargs):
tk.Tk.__init__(root, *args, **kwargs)
root.title("trial")
root.geometry("430x830")
root.configure(bg="lightblue")
#setup image
root.photo1 = tk.PhotoImage(file="fish.gif") #defines a photo and gives the file name
root.label1 = tk.Label(root, image=root.photo1)#puts label in the window in this case not text file must be in program folder
root.label1.place(x=0, y=0) #says how to place the label
#setup display high sensor label
root.photo2 = tk.PhotoImage(file='oraled.png')
root.dishisensorlabel= tk.Label(root, image="")
root.dishisensorlabel.place(x=210, y=500)
labeldishisensor= tk.Label(root,text=("Display High"), font=(ftb), fg="black")
labeldishisensor.place(x=0, y=500)
root.update_gpio()
def update_gpio(root):
dhsen =('fish.gif' if GPIO.input(5) else 'oraled')
root.dishisensorlabel.configure(image=dhsen)
root.after(200, root.update_gpio)
if __name__== "__main__":
app = gpio()
app.mainloop()
我收到以下错误 我正在 运行 在 thorney 删除脚本时
>>> %Run 'use of images for leds.py'
Traceback (most recent call last):
File "/home/pi/use of images for leds.py", line 63, in <module>
app = gpio()
File "/home/pi/use of images for leds.py", line 55, in __init__
root.update_gpio()
File "/home/pi/use of images for leds.py", line 58, in update_gpio
root.dishisensorlabel.configure(image=dhsen)
File "/usr/lib/python3.7/tkinter/__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File "/usr/lib/python3.7/tkinter/__init__.py", line 1476, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "fish.gif" doesn't exist
>>>
任何帮助将不胜感激
感谢您表现出兴趣,现在可以使用了,我希望它能帮助其他尝试类似脚本的人
import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#Setup input GPIO pins for use with optical infrared sensors
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP LOW WATER LEVEL SENSOR
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP NORM WATER LEVEL SENSOR
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP HIGH WATER LEVEL SENSOR
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP) # RO LOW WATER LEVEL SENSOR
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY HIGH WATER LEVEL
#GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # MAY USE AS PROTIEN SKIMMER SENSOR
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY LOW WATER LEVEL SENSOR
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT) #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT) #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT) #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT) #used for return pump 1
GPIO.setup(9, GPIO.OUT) #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT) #power head 1 ch
GPIO.setup(7, GPIO.OUT) # AUTO TOP OFF PUMP
#Setup output GPIO pins for use with Red Yellow and Green LEDS
GPIO.setup(23, GPIO.OUT) # DISPLAY HIGH WATER LEVEL RED LED
GPIO.setup(24, GPIO.OUT) # DISPLAY NORM WATER LEVEL GREEN LED
GPIO.setup(25, GPIO.OUT) # DISPLAY LOW WATER LEVEL YELLOW LED
GPIO.setup(12, GPIO.OUT) # AUTO TOP OFF LOW LEVEL RED LED
GPIO.setup(16, GPIO.OUT) # SUMP HIGH WATER LEVEL RED LED
GPIO.setup(20, GPIO.OUT) # SUMP NORMAL WATER LEVEL GREEN LED
GPIO.setup(21, GPIO.OUT) # SUMP LOW WATER LEVEL YELLOW LED
#setup fonts
ftb= 'Verdana', 12, 'bold'
#setup class for the tk program
class gpio(tk.Tk):
def __init__(root, *args, **kwargs):
tk.Tk.__init__(root, *args, **kwargs)
root.title("trial")
root.geometry("430x830")
root.configure(bg="lightblue")
root.photo2= tk.PhotoImage(file="whiteled.png")
root.photo3= tk.PhotoImage(file='green.png')
#setup image
root.photo1 = tk.PhotoImage(file="fish.gif") #defines a photo and gives the file name
root.label1 = tk.Label(root, image=root.photo1)#puts label in the window in this case not text file must be in program folder
root.label1.place(x=0, y=0) #says how to place the label
#setup display high sensor label
root.dishisensorlabel= tk.Label(root, text="")
root.dishisensorlabel.place(x=210, y=500)
labeldishisensor= tk.Label(root,text=("Power Head 2"), font=(ftb), fg="black")
labeldishisensor.place(x=0, y=500)
root.update_gpio()
def update_gpio(root):
dhsen =(root.photo3 if GPIO.input(9) else root.photo2)
root.dishisensorlabel.configure(image=dhsen, text="")
root.after(200, root.update_gpio)
if __name__== "__main__":
app = gpio()
app.mainloop()
需要进行一些更改。
final code
import tkinter as tk
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#Setup input GPIO pins for use with optical infrared sensors
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP LOW WATER LEVEL SENSOR
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP NORM WATER LEVEL SENSOR
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) # SUMP HIGH WATER LEVEL SENSOR
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP) # RO LOW WATER LEVEL SENSOR
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY HIGH WATER LEVEL
#GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # MAY USE AS PROTIEN SKIMMER SENSOR
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # DISPLAY LOW WATER LEVEL SENSOR
#setup output pins for relay control
GPIO.setup(2, GPIO.OUT) #return pump 1 ch 1 cutoff relay in series with GPIO(8)
GPIO.setup(3, GPIO.OUT) #return pump 2 ch 2 relay
GPIO.setup(4, GPIO.OUT) #gyre pump ch3 relay
GPIO.setup(8, GPIO.OUT) #used for return pump 1
GPIO.setup(9, GPIO.OUT) #power head 2 ch 5 relay
GPIO.setup(11, GPIO.OUT) #power head 1 ch
GPIO.setup(7, GPIO.OUT) # AUTO TOP OFF PUMP
#Setup output GPIO pins for use with Red Yellow and Green LEDS
GPIO.setup(23, GPIO.OUT) # DISPLAY HIGH WATER LEVEL RED LED
GPIO.setup(24, GPIO.OUT) # DISPLAY NORM WATER LEVEL GREEN LED
GPIO.setup(25, GPIO.OUT) # DISPLAY LOW WATER LEVEL YELLOW LED
GPIO.setup(12, GPIO.OUT) # AUTO TOP OFF LOW LEVEL RED LED
GPIO.setup(16, GPIO.OUT) # SUMP HIGH WATER LEVEL RED LED
GPIO.setup(20, GPIO.OUT) # SUMP NORMAL WATER LEVEL GREEN LED
GPIO.setup(21, GPIO.OUT) # SUMP LOW WATER LEVEL YELLOW LED
#setup fonts
ftb= 'Verdana', 12, 'bold'
#setup class for the tk program
class gpio(tk.Tk):
def __init__(root, *args, **kwargs):
tk.Tk.__init__(root, *args, **kwargs)
root.title("Heads Up")
root.geometry("430x830")
root.Greenoff= tk.PhotoImage(file="greenoff.png")
root.Greenon= tk.PhotoImage(file="GreenOnworking.png")
root.Yellowoff= tk.PhotoImage(file="yellowoff.png")
root.Yellowon= tk.PhotoImage(file="yellowon.png")
root.Redoff= tk.PhotoImage(file="redoff.png")
root.Redon= tk.PhotoImage(file="redon.png")
root.pumpoffnew= tk.PhotoImage(file="pumpoffnew.png")
root.pumponnew= tk.PhotoImage(file="pumponnew.png")
root.pumpoffr= tk.PhotoImage(file="pumpoffr.png")
root.pumponr= tk.PhotoImage(file="pumponr.png")
root.pwrhdon= tk.PhotoImage(file="pwrhdon.png")
root.pwrhdoff= tk.PhotoImage(file="pwrhdoff.png")
root.gyreon= tk.PhotoImage(file="gyreon.png")
root.gyreoff= tk.PhotoImage(file="gyreoff.png")
root.sensorin= tk.PhotoImage(file="sensorin.png")
root.sensorout= tk.PhotoImage(file="sensorout.png")
root.lowsensorin= tk.PhotoImage(file="lowsensorin.png")
root.lowsensorout= tk.PhotoImage(file="lowsesorout.png")
root.normsensorin= tk.PhotoImage(file="normsensorin.png")
root.normsensorout= tk.PhotoImage(file="normsesorout.png")
root.highsensorin= tk.PhotoImage(file="highsensorin.png")
root.disphighsensorout= tk.PhotoImage(file="disphighsesorout.png")
#setup image
root.photo1 = tk.PhotoImage(file="background.png") #defines a photo and gives the file name
root.label1 = tk.Label(root, image=root.photo1)#puts label in the window in this case not text file must be in program folder
root.label1.place(x=0, y=0) #says how to place the label
#setup exit button
Exitbutton= tk.Button(root, text="Exit", font=(ftb), width=5, bg="red", fg="white", command=root.destroy)
Exitbutton.place(x=20, y=600)
# setup display water high light
root.dishighlabel =tk.Label(root,)
root.dishighlabel.place(x=291, y=91)
# setup display water norm light
root.disnormlabel =tk.Label(root,)
root.disnormlabel.place(x=203, y=91)
# setup display water low light
root.dislowlabel =tk.Label(root,)
root.dislowlabel.place(x=114, y=91)
# setup sump water high light
root.sumphighlabel =tk.Label(root,)
root.sumphighlabel.place(x=218, y=287)
# setup sump water norn light
root.sumpnormlabel=tk.Label(root,)
root.sumpnormlabel.place(x=154, y=287)
root.sumplowlabel=tk.Label(root,)
root.sumplowlabel.place(x=90, y=287)
# setup Ato water low light label
root.atopledsensorlabel=tk.Label(root,)
root.atopledsensorlabel.place(x=337, y=287)
#set up return pump 1 labels
root.rtnp1label = tk.Label(root,)
root.rtnp1label.place(x=45, y=330)
#setup return pump 2 labels
root.rtnp2label = tk.Label(root,)
root.rtnp2label.place(x=250, y=330)
#setup gyre pump labels
root.gyrelabel =tk.Label(root, text="")
root.gyrelabel.place(x=357, y=65)
#setup powerhead 1 labels
root.pwrhd1label = tk.Label(root, text="")
root.pwrhd1label.place(x=43, y=65)
#setup powerhead 2 labels
root.pwrhd2label = tk.Label(root, text="")
root.pwrhd2label.place(x=43, y=95)
#setup ato pump labels
root.atoppumplabel = tk.Label(root, text="")
root.atoppumplabel.place(x=312, y=330)
#setup display high sensor label
root.dishisensorlabel= tk.Label(root, text="")
root.dishisensorlabel.place(x=0, y=30)
#setup display low sensor label
root.dislosensorlabel= tk.Label(root, text="")
root.dislosensorlabel.place(x=0, y=82)
#setup sump high sensor label
root.sumhisensorlabel= tk.Label(root, text="")
root.sumhisensorlabel.place(x=0, y=227)
#setup sump norm sensor label
root.sumnorsensorlabel= tk.Label(root, text="")
root.sumnorsensorlabel.place(x=0, y=279)
#setup sump low sensor label
root.sumlowsensorlabel= tk.Label(root, text="")
root.sumlowsensorlabel.place(x=0, y=332)
#setup ato sensor label
root.atopsensorlabel= tk.Label(root, text="")
root.atopsensorlabel.place(x=399, y=335)
# start the GPIO checks"
root.update_gpio()
def update_gpio(root):
#sensors
dhsen =(root.disphighsensorout if GPIO.input(5) else root.highsensorin)
root.dishisensorlabel.configure(image=dhsen,)
dlsen =(root.lowsensorout if GPIO.input(13) else root.lowsensorin)
root.dislosensorlabel.configure(image=dlsen,)
shsen =(root.disphighsensorout if GPIO.input(22) else root.highsensorin)
root.sumhisensorlabel.configure(image=shsen,)
snsen =(root.normsensorout if GPIO.input(27) else root.normsensorin)
root.sumnorsensorlabel.configure(image=snsen,)
slsen =(root.lowsensorout if GPIO.input(17) else root.lowsensorin)
root.sumlowsensorlabel.configure(image=slsen,)
atosen =(root.sensorout if GPIO.input(26) else root.sensorin)
root.atopsensorlabel.configure(image=atosen,)
#leds
atop = (root.Redon if GPIO.input(12) else root.Greenon)
root.atopledsensorlabel.configure(image=atop,)
sumh = (root.Redon if GPIO.input(16) else root.Redoff)
root.sumphighlabel.configure(image=sumh,)
sumn = (root.Greenon if GPIO.input(20) else root.Greenoff)
root.sumpnormlabel.configure(image=sumn,)
suml = (root.Yellowon if GPIO.input(21) else root.Yellowoff)
root.sumplowlabel.configure(image=suml,)
dish = (root.Redon if GPIO.input(23) else root.Redoff)
root.dishighlabel.configure(image=dish,)
disn = (root.Greenon if GPIO.input(24) else root.Greenoff)
root.disnormlabel.configure(image=disn,)
disl = (root.Yellowon if GPIO.input(25) else root.Yellowoff)
root.dislowlabel.configure(image=disl,)
#pumps
rt1 = (root.pumponnew if GPIO.input(2) else root.pumpoffnew)
root.rtnp1label.configure(image=rt1,)
rt2 = (root.pumponr if GPIO.input(3) else root.pumpoffr)
root.rtnp2label.configure(image=rt2,)
ph2 = (root.pwrhdon if GPIO.input(9) else root.pwrhdoff)
root.pwrhd2label.configure(image=ph2,)
ph1 = (root.pwrhdon if GPIO.input(11) else root.pwrhdoff)
root.pwrhd1label.configure(image=ph1,)
gyr = (root.gyreon if GPIO.input(4) else root.gyreoff)
root.gyrelabel.configure(image=gyr,)
top1 =(root.pumpoffnew if GPIO.input(7) else root.pumponnew)
root.atoppumplabel.configure(image=top1,)
# call this function again in one second
root.after(1000, root.update_gpio)
if __name__== "__main__":
app = gpio()
app.mainloop()
希望这可以帮助其他人尝试做这种事情。