So I'm trying to create a UI that allows multiple buttons in the same window. All of the are toggles to control GPIO outputs to control relays. I've managed to get them to all work individually in their own files, but when put into the same file is all on or all off. There is only 4 toggles for now, as a demonstration for my boss, but there will be many more on the final product.`

Below is the code I've come up with so far. (keep in mind I only started learning Python and coding in general yesterday) I'm on a bit of a time crunch and have used the search function. Thats the only way I've gotten this far. Thanks in Advance!!

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

try:
import Tkinter as tk
except ImportError:
import tkinter as tk

from Tkinter import *

win=Tk()
win.title("Q")

def toggle():
    if b1.config('text')[-1] == 'AMBIENT':
    b1.config(text='OFF')
    GPIO.setup(24, GPIO.OUT)
    GPIO.output(24, GPIO.LOW)

else:
    b1.config(text='AMBIENT')
    GPIO.output(24, GPIO.HIGH)

if b2.config('text')[-1] == 'CEILING':
    b2.config(text='OFF')
    GPIO.setup(18, GPIO.OUT)
    GPIO.output(18, GPIO.LOW)

else:
    b2.config(text='CEILING')
    GPIO.output(18, GPIO.HIGH)

b1 = Button(win, text="AMBIENT", command=toggle)
b2 = Button(win, text="CEILING", command=toggle)
b3 = Button(win, text="LASER", command=toggle)
b4 = Button(win, text="RADIO", command=toggle)

b1.pack(pady=2)
b2.pack(pady=2)
b3.pack(pady=2)
b4.pack(pady=2)

mainloop()
rproffitt commented: Keep guessing to a minimum. Add tags or just tell us this is some Arduino. +15

Recommended Answers

All 4 Replies

Generlly, you send the button number to the function. Also, you import Tkinter twice. Which one is the program supposed to use?

Unfortunately, in this application it still turns all of the GPIO's assigned on or off at the same time. I'm pretty sure it has something to do with the conditions that turn the switches on and off. I'm currently doing all the research possible to find other alternatives or new code. I know its possible as i've seen it before, just not on the coding side of it.

Using your last pastebin, the embedded coder in me tells me that line 12 needs to be either moved or duplicated.

I think I would move that line to after the GPIO.output() statements. Why? Because in one case you didn't declare the GPIO.setup() before you set it high or low so you might send a pulse out that pin. And in another case you may be setting the pin HIGH but since GPIO.setup() was never called it doesn't work.

So my view is on startup, GPIO.setup() those pins and default state so you know what power up condition they should be at and in most systems you don't change GPIO.setup() during a run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.