Увы извилин не хватило на Ваш код. Мне необходима помощь в корректировки кода над одной кнопкой.
Для облегчения опишу сокращения которые я использовал в коде.
but1 до but9 - название создаваемых графических кнопок.
buta1 до buta9 - это код выполняемый при нажатии кнопки мыши на графическую кнопку
rele1 до rele8 - переменные для использования их как я пытался в случае с def buta1(event):
Цель программы клик по одной из 8 графическим кнопкам для управления реле. Сравнения при нажатии если реле (если реле включено то отключить и сменить его надпись и цвет) противоположно при отключённом состоянии.
from time import sleep
from tkinter import *
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN)
GPIO.setup(18, GPIO.IN)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(35, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
root = Tk()
rele1 = int(0)
rele2 = int(0)
rele3 = int(0)
rele4 = int(0)
rele5 = int(0)
rele6 = int(0)
rele7 = int(0)
rele8 = int(0)
z = int(1)
def buta1(event):
for rele1 in range (0, 1):
if rele1 == 0:
print ("RELE 1 - ON")
GPIO.output(11, 0)
but1.configure(text='Rele 1 - ON',bg='green',fg='red',font='arial 16')
rele1 = rele1 + 1
break
else:
print ("RELE 1 - OFF")
GPIO.output(11, 1)
but1.configure(text='Rele 1 - OFF',bg='black',fg='red',font='arial 16')
rele1 = rele1 - 1
def buta2(event):
print ("RELE 2 - ON")
GPIO.output(12, 0)
but2.configure(text='Rele 2 - ON',bg='green',fg='red',font='arial 16')
def buta3(event):
print ("RELE 3 - ON")
GPIO.output(13, 0)
but3.configure(text='Rele 3 - ON',bg='green',fg='red',font='arial 16')
def buta4(event):
print ("RELE 4 - ON")
GPIO.output(15, 0)
but4.configure(text='Rele 4 - ON',bg='green',fg='red',font='arial 16')
def buta5(event):
print ("RELE 5 - ON")
GPIO.output(31, 0)
but5.configure(text='Rele 5 - ON',bg='green',fg='red',font='arial 16')
def buta6(event):
print ("RELE 6 - ON")
GPIO.output(33, 0)
but6.configure(text='Rele 6 - ON',bg='green',fg='red',font='arial 16')
def buta7(event):
print ("RELE 7 - ON")
GPIO.output(35, 0)
but7.configure(text='Rele 7 - ON',bg='green',fg='red',font='arial 16')
def buta8(event):
print ("RELE 8 - ON")
GPIO.output(37, 0)
but8.configure(text='Rele 8 - ON',bg='green',fg='red',font='arial 16')
def buta9(event):
print ("SYSTEM OFF")
but9.configure(bg='red')
GPIO.output(11, GPIO.HIGH)
sleep(0.1)
GPIO.output(12, GPIO.HIGH)
sleep(0.1)
GPIO.output(13, GPIO.HIGH)
sleep(0.1)
GPIO.output(15, GPIO.HIGH)
sleep(0.1)
GPIO.output(31, GPIO.HIGH)
sleep(0.1)
GPIO.output(33, GPIO.HIGH)
sleep(0.1)
GPIO.output(35, GPIO.HIGH)
sleep(0.1)
GPIO.output(37, GPIO.HIGH)
sleep(0.1)
but1.configure(text='Rele 1 - OFF',bg='black',fg='red',font='arial 16')
but2.configure(text='Rele 2 - OFF',bg='black',fg='red',font='arial 16')
but3.configure(text='Rele 3 - OFF',bg='black',fg='red',font='arial 16')
but4.configure(text='Rele 4 - OFF',bg='black',fg='red',font='arial 16')
but5.configure(text='Rele 5 - OFF',bg='black',fg='red',font='arial 16')
but6.configure(text='Rele 6 - OFF',bg='black',fg='red',font='arial 16')
but7.configure(text='Rele 7 - OFF',bg='black',fg='red',font='arial 16')
but8.configure(text='Rele 8 - OFF',bg='black',fg='red',font='arial 16')
but9.configure(text='RESET',bg='black',fg='red',font='arial 16')
but1 = Button(root, text='Rele 1 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but1.bind("<Button-1>",buta1)
but1.grid(row=0,column=1)
but1.pack(side=TOP)
but2 = Button(root, text='Rele 2 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but2.bind("<Button-1>",buta2)
but2.grid(row=1,column=1)
but2.pack(side=TOP)
but3 = Button(root, text='Rele 3 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but3.bind("<Button-1>",buta3)
but3.grid(row=2,column=1)
but3.pack(side=TOP)
but4 = Button(root, text='Rele 4 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but4.bind("<Button-1>",buta4)
but4.grid(row=3,column=1)
but4.pack(side=TOP)
but5 = Button(root, text='Rele 5 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but5.bind("<Button-1>",buta5)
but5.grid(row=4,column=1)
but5.pack(side=TOP)
but6 = Button(root, text='Rele 6 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but6.bind("<Button-1>",buta6)
but6.grid(row=5,column=1)
but6.pack(side=TOP)
but7 = Button(root, text='Rele 7 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but7.bind("<Button-1>",buta7)
but7.grid(row=6,column=1)
but7.pack(side=TOP)
but8 = Button(root, text='Rele 8 - OFF',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but8.bind("<Button-1>",buta8)
but8.grid(row=7,column=1)
but8.pack(side=TOP)
but9 = Button(root, text='RESET',width=25,heigh=2,bg='black',fg='red',font='arial 16')
but9.bind("<Button-1>",buta9)
but9.pack(side=TOP)
root.mainloop()