Hello just recently decided to get back into this after 20+ years of not touching it. I bought a Rasberry Pi 4B+, and 8 channel relay board with the hopes of making a touch screen controller to control things on my jeep JKU (lights, winch, compressor, etc..). Over the last 3 days i have now wrote enough code to go from, just testing all 8 relays are working in correct order , to turning each relay on with push button switches, and now im writing code for a GUI. I have 10 buttons on my GUI, one for each of the 8 relays to activate and deactivate the relay, as well as change the text and color of the buttons to say on or off...etc I also have an exit button that closes the menu and exits the program cleanly. Im having issues with my 10th button which I would like to group multiple outputs together ( say for all on or off, or front lights, rear lights, etc...) When ever I try to group multiple outputs together I get this error. " AttributeError: 'list' object has no attribute 'is_lit'" I have tried everything I can think of and cannot get it to understand I want to control multiple outputs.
Here is a section of the code that is working correctly.
rly1Button= Button( win, text = 'Relay 1 ON', font = myFont, command = rly1Toggle, bg = 'green', height = 1, width = 24)
rly1Button.grid(row=1, column=0)
def rly1Toggle():
if rly1.is_lit:
rly1.off()
rly1Button["text"] = "1 OFF"
rly1Button["bg"] = "green"
else:
rly1.on()
rly1Button["text"] = "1 ON"
rly1Button["bg"] = "red"
rly1 = LED(23)
rly2 = LED(22)
rly3 = LED(21)
rly4 = LED(20)
rly5 = LED(19)
rly6 = LED(18)
rly7 = LED(17)
rly8 = LED(16)
from tkinter import *
import tkinter.font
from gpiozero import LED
import RPi.GPIO
RPi.GPIO.setmode(RPi.GPIO.BCM)
I am having issues with this part
ALLButton= Button( win, text = 'ALL ON', font = myFont, command = ALLToggle, bg = 'green', height = 1, width = 24)
ALLButton.grid(row=6, column=0)
def ALLToggle():
if ALL.is_lit:
ALL.off()
ALLButton["text"] = "ALL OFF"
ALLButton["bg"] = "green"
else:
ALL.on()
ALLButton["text"] = "ALL ON"
ALLButton["bg"] = "red"
ALL = [rly1, rly2, rly3, rly4, rly5, rly6, rly7, rly8]
I know im probably writing code that way to long and very simple, but im taking baby steps lol....Any idea on how to toggle all the GPIOs on and off with out getting an attribute error?
I cant figure out how to post the whole file either. I tried making a github account but not sure anyone can access it.
https://github.com/joegibbstruck/Start- ... ain/GUI.py
Any help would be greatly appreciated.
-
- Posts: 5
- Joined: Tue Sep 26, 2023 2:01 am
Re: New user looking for a little help with python programming.
To post code on the forum you need to use code tags to preserve the indentation that python relies on to work
You just need to copy your code in to your post and add code tags like this ( the bold bits are what you add )
Add [code] at the top
for i in range(8):
print "code goes here"
time.sleep(1)
and [/code] at the bottom.
which results in it looking like this
You can also use the code button to add code tags to a block of highlighted code as described in this post
viewtopic.php?t=84477
You just need to copy your code in to your post and add code tags like this ( the bold bits are what you add )
Add [code] at the top
for i in range(8):
print "code goes here"
time.sleep(1)
and [/code] at the bottom.
which results in it looking like this
Code: Select all
for i in range(8):
print "code goes here"
time.sleep(1)
viewtopic.php?t=84477
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported
The use of crystal balls & mind reading are not supported
Re: New user looking for a little help with python programming.
joegibbstruck wrote: ↑Tue Sep 26, 2023 2:30 amI tried making a github account but not sure anyone can access it.
https://github.com/joegibbstruck/Start- ... ain/GUI.py
Any help would be greatly appreciated.
To confirm that URL results in " 404 This is not the web page you are looking for "
But there is nothing for Github to display. Looking at the top level of your github account we see, at https://github.com/joegibbstruck
"joegibbstruck doesn't have any public repositories yet."
Beware of the Leopard
Re: New user looking for a little help with python programming.
You will need to share ALL of your code. (Correctly formatted, as discussed above.)joegibbstruck wrote: ↑Tue Sep 26, 2023 2:30 amWhen ever I try to group multiple outputs together I get this error. " AttributeError: 'list' object has no attribute 'is_lit'" I
From the snippets you have posted your ALL object is a python list so will only - by default - respond to methods that are valid for a list.
You haven't provided any code to show how you have defined your is_lit function.
"Programming by wishful thinking" can be a useful way of making progress. But at some point you have to make the wishes concrete by writing code to implement the intention.
Beware of the Leopard
-
- Posts: 5
- Joined: Tue Sep 26, 2023 2:01 am
Re: New user looking for a little help with python programming.
Sorry had the repository set to private, its now public and you should be able to see the code
https://github.com/joegibbstruck/Start- ... relays.git
I am using imports from gpiozero and driving the relays off of a led_ command... Probably not the correct way to do it, but it works.
I just cannot figure out how to make the list work so I can turn multiple things on and off. I had to make a list in my earlier project using pushbuttons and was able to call it things and define things as on or off... but I cannot figure out how to get them to toggle at all. I know these are very noobish questions, and have tried for about 6hrs to figure it out on my own. Just looking for a little guidance.
https://github.com/joegibbstruck/Start- ... relays.git
I am using imports from gpiozero and driving the relays off of a led_ command... Probably not the correct way to do it, but it works.
I just cannot figure out how to make the list work so I can turn multiple things on and off. I had to make a list in my earlier project using pushbuttons and was able to call it things and define things as on or off... but I cannot figure out how to get them to toggle at all. I know these are very noobish questions, and have tried for about 6hrs to figure it out on my own. Just looking for a little guidance.
Re: New user looking for a little help with python programming.
It may work now. But using TWO gpio-activating libraries (gpiozero and RPi.GPIO) in the same script is likely to cause you headaaches in future.joegibbstruck wrote: ↑Tue Sep 26, 2023 3:02 pmSorry had the repository set to private, its now public and you should be able to see the code
https://github.com/joegibbstruck/Start- ... relays.git
I am using imports from gpiozero and driving the relays off of a led_ command... Probably not the correct way to do it, but it works.
I strongly suggest that you re-work your script to use elements from gpiozero only.
Having done so you might be able to use LEDBoard as a way of manipulating a group of connections at the same time.
Beware of the Leopard
-
- Posts: 5
- Joined: Tue Sep 26, 2023 2:01 am
Re: New user looking for a little help with python programming.
So with the LEDboard I can make work by itself in a separate script the way I want, but when I try combining that script with my existing one, I get a GPIO in use error. saying there is another object on that pin already. it's because I already have those GPIOs assigned to individual relays, at least I think it is anyway. Maybe if I rewrite all the old stuff to just refence certain LEDs from the LEDboard list?
Also as far as the RPi.GPIO I'm only using it to do 2 things, one is to assign GPIOs based on their name number instead of board number, that easy to change, the other is a GPIO.cleanup...is there a comparable gpiozero code for this?
Also as far as the RPi.GPIO I'm only using it to do 2 things, one is to assign GPIOs based on their name number instead of board number, that easy to change, the other is a GPIO.cleanup...is there a comparable gpiozero code for this?
Re: New user looking for a little help with python programming.
Yes to pin naming conventions.
I think the cleanup is done by default with gpiozero.
I really recommend reading the supporting documentation for gpiozero.
https://gpiozero.readthedocs.io/en/stab ... -numbering
https://gpiozero.readthedocs.io/en/stab ... io-cleanup
I think the cleanup is done by default with gpiozero.
I really recommend reading the supporting documentation for gpiozero.
https://gpiozero.readthedocs.io/en/stab ... -numbering
https://gpiozero.readthedocs.io/en/stab ... io-cleanup
Beware of the Leopard
-
- Posts: 5
- Joined: Tue Sep 26, 2023 2:01 am
Re: New user looking for a little help with python programming.
Thanks for that help, I was able to rewrite everything using only gpiozero, I can call certain relays or all on and off using the led board. I added the gpiozero coding for clearing gpios and closing a window. it all works beautifully on that side.
In the process I gave up my widget boxes changing text and color when they are pressed. I had them programmed to change with definitions linked to the toggle function for each relay. I can no longer have to define toggle( I guess something to do with LEDBoard), so that doesn't work anymore, I'm sure I can just modify the Button widgets somehow to correct this. The basics are working how I want them now so that's not bad for my 4th day back into this.
This is how I learn, fix something break something, figure out how to fix that.
In the process I gave up my widget boxes changing text and color when they are pressed. I had them programmed to change with definitions linked to the toggle function for each relay. I can no longer have to define toggle( I guess something to do with LEDBoard), so that doesn't work anymore, I'm sure I can just modify the Button widgets somehow to correct this. The basics are working how I want them now so that's not bad for my 4th day back into this.
This is how I learn, fix something break something, figure out how to fix that.
-
- Posts: 5
- Joined: Tue Sep 26, 2023 2:01 am
Re: New user looking for a little help with python programming.
Ok so I now have the basic function working the way I want it to with two exceptions. I can press any button and toggle the output on/off, and I can control multiple outputs on and off, so I can turn all relays off. Works beautifully, mechanically speaking. However if I turn switches on and then hit the button to turn everything off, the buttons still show on, but the relays turn off.
I think I need to figure out a way for the config of the button to recognize the state of the output, or something along that line. Is there a way to make that work?
Also what would be the correct code to shut down a raspberry pi via button on screen? I have the button made but the code I got off the inter-web does not work for that function.
and lastly, what code would I use to automatically boor to this program on startup?
Ultimately the goal is to leave this in my jeep, to run all my accessories as well as monitor my tuning software. So i need to be able to shut it down safely, and it would be nice it if boots to this program on start up.
here is my latest program, it needs allot of refining but im getting it where i need it.
https://github.com/joegibbstruck/Start- ... 6b/GUI1.py
I think I need to figure out a way for the config of the button to recognize the state of the output, or something along that line. Is there a way to make that work?
Also what would be the correct code to shut down a raspberry pi via button on screen? I have the button made but the code I got off the inter-web does not work for that function.
and lastly, what code would I use to automatically boor to this program on startup?
Ultimately the goal is to leave this in my jeep, to run all my accessories as well as monitor my tuning software. So i need to be able to shut it down safely, and it would be nice it if boots to this program on start up.
here is my latest program, it needs allot of refining but im getting it where i need it.
https://github.com/joegibbstruck/Start- ... 6b/GUI1.py
Re: New user looking for a little help with python programming.
joegibbstruck wrote: ↑Thu Sep 28, 2023 3:16 amwhat code would I use to automatically boor to this program on startup?
You may find help in one of these volunteer-provided forum resources -
STICKY: Running A Program At Start UP A Beginner's Guide viewtopic.php?t=314455
STICKY: How to use Autostart - Raspberry Pi OS (Desktop) viewtopic.php?t=294014
Beware of the Leopard