Hi, I have been working on some code in Tkinter - this is my first ever coding in python and have basically copied chunks of code over to see what is possible but I am stuck on one section - I am trying to create a GUI in Tkinter but I am having a problem with this section:

def check_button():
    if (GPIO.input(13) == GPIO.LOW):
        labelText.set("FRONT LIGHTS ON ")
    else:
        labelText.set("FRONT LIGHTS OFF")
    root.after(10,check_button)

Basically I want to use this section of code more than once but I can't work out how to make it work, the original section of code works fine but when I try to add it in a second time it doesn't work, I can get it to appear correctly it just doesn't do anything when I click the button - below is my full code - any help would be gratefully appreciated - I have spent hours searching the forums but have struggled to find this exact problem.

from Tkinter import *

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)

def say_ALLON():
    print "ALL LIGHTS ON"
    GPIO.output(7, GPIO.LOW)
    GPIO.output(11, GPIO.LOW)
    GPIO.output(13, GPIO.LOW)
    GPIO.output(15, GPIO.LOW)
    GPIO.output(18, GPIO.LOW)

def say_ALLOFF():
    print "ALL LIGHTS OFF"
    GPIO.output(7, GPIO.HIGH)
    GPIO.output(11, GPIO.HIGH)
    GPIO.output(13, GPIO.HIGH)
    GPIO.output(15, GPIO.HIGH)
    GPIO.output(18, GPIO.HIGH)


def say_FSON():
    print "FRONT LIGHTS ON"
    GPIO.output(7, GPIO.LOW)
def say_FSOFF():
    print "FRONT LIGHTS OFF"
    GPIO.output(7, GPIO.HIGH)

def check_button():
    if (GPIO.input(13) == GPIO.LOW):
        labelText.set("FRONT LIGHTS ON ")
    else:
        labelText.set("FRONT LIGHTS OFF")
    root.after(10,check_button)


def say_RSON():
    print "REAR LIGHTS ON"
    GPIO.output(13, GPIO.LOW)
def say_RSOFF():
    print "REAR LIGHTS OFF"
    GPIO.output(13, GPIO.HIGH)   

def check_button():
    if (GPIO.input(13) == GPIO.LOW):
        labelText.set("REAR LIGHTS ON ")
    else:
        labelText.set("REAR LIGHTS OFF")
    root.after(10,check_button)


root = Tk()

button = Button(root, text="Quit.", fg="red", command=quit)
button.grid(row=5, column=6)

FSON_there = Button(root, text="FRONT LIGHTS ON!", command=say_FSON, height = 8, width = 25)
FSON_there.grid(row=2, column=2)

FSOFF_there = Button(root, text="FRONT LIGHTS OFF!", command=say_FSOFF, height = 8, width = 25)
FSOFF_there.grid(row=2, column=3)

RSON_there = Button(root, text="REAR LIGHTS ON!", command=say_RSON, height = 8, width = 25)
RSON_there.grid(row=3, column=2)

RSOFF_there = Button(root, text="REAR LIGHTS OFF!", command=say_RSOFF, height = 8, width = 25)
RSOFF_there.grid(row=3, column=3)

ALLON_there = Button(root, text="ALL ON!", command=say_ALLON, height = 8, width = 25)
ALLON_there.grid(row=4, column=2)

ALLOFF_there = Button(root, text="ALL OFF!", command=say_ALLOFF, height = 8, width = 25)
ALLOFF_there.grid(row=4, column=3)

labelText = StringVar()
labelText.set("REAR LIGHTS ON")
button1 = Label(root, textvariable=labelText, height=4)
button1.grid(row=3, column=4)

root.title("CONTROL SYSTEM")
root.geometry('500x300+200+200')

root.after(10,check_button)
root.mainloop()

You have check_button() declared twice in the code above. One of them should be deleted. A proof of concept to show that the code is working as programmed (although possibly not as you want). Add a print statement to show the values for GPIO.input(13) and GPIO.LOW. Possibly the else statement is hit every time so it looks like nothing is happening.

from Tkinter import *

##import time

def say_ALLON():
    print "ALL LIGHTS ON"

def say_ALLOFF():
    print "ALL LIGHTS OFF"


def say_FSON():
    print "FRONT LIGHTS ON"

def say_FSOFF():
    print "FRONT LIGHTS OFF"

def check_button():
    print "check_button"
    if labelText.on_off:
        labelText.set("REAR LIGHTS ON")
    else:
        labelText.set("LIGHTS OFF")
    labelText.on_off = not labelText.on_off
    root.after(500, check_button)


def say_RSON():
    print "REAR LIGHTS ON"

def say_RSOFF():
    print "REAR LIGHTS OFF"



root = Tk()

button = Button(root, text="Quit.", fg="red", command=quit)
button.grid(row=5, column=3)

FSON_there = Button(root, text="FRONT LIGHTS ON!", command=say_FSON, height = 8, width = 25)
FSON_there.grid(row=2, column=2)

FSOFF_there = Button(root, text="FRONT LIGHTS OFF!", command=say_FSOFF, height = 8, width = 25)
FSOFF_there.grid(row=2, column=3)

RSON_there = Button(root, text="REAR LIGHTS ON!", command=say_RSON, height = 8, width = 25)
RSON_there.grid(row=3, column=2)

RSOFF_there = Button(root, text="REAR LIGHTS OFF!", command=say_RSOFF, height = 8, width = 25)
RSOFF_there.grid(row=3, column=3)

ALLON_there = Button(root, text="ALL ON!", command=say_ALLON, height = 8, width = 25)
ALLON_there.grid(row=4, column=2)

ALLOFF_there = Button(root, text="ALL OFF!", command=say_ALLOFF, height = 8, width = 25)
ALLOFF_there.grid(row=4, column=3)

labelText = StringVar()
labelText.set("REAR LIGHTS ON")
button1 = Label(root, textvariable=labelText, height=4)
button1.grid(row=3, column=4)

root.title("CONTROL SYSTEM")
root.geometry('700x500+200+200')

labelText.on_off=True
root.after(10,check_button)
root.mainloop()

Edited 2 Years Ago by woooee

This article has been dead for over six months. Start a new discussion instead.