1

I am struggling with tkinter on rpi using python3. Below follow my code, I am getting an error: button not named. I did the same code on python2 using root not self as argument my leds work perfectly. Althought using self I could only call a command without using GPIO. Could someone help me? I would be grateful.

#!/usr/bin/python

import time
import RPi.GPIO as GPIO
from tkinter import *
import tkinter as tkenter code here

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(5, GPIO.OUT) #Luminária A
GPIO.setup(6, GPIO.OUT) #Luminária B
GPIO.setup(13, GPIO.IN) #Luz ambiente no setor
GPIO.setup(19, GPIO.IN) #Pessoas no setor

GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)

LARGE_FONT = ("Verdana", 12)


def leD():

    if ((GPIO.input(5)) and (GPIO.input(6))):
        GPIO.output(5, GPIO.LOW)
        GPIO.output(6, GPIO.LOW)
        showAcionamento["text"]="Lights on"

    else:
        GPIO.output(5, GPIO.HIGH)
        GPIO.output(6, GPIO.HIGH)
        showAcionamento["text"]="Lights off"

def sairr():
    GPIO.cleanup()
    exit()


class showAcionam(tk.Tk):


    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = Acionamento(container, self)

        self.frames[Acionamento] = frame

        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Acionamento)


    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class Acionamento(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Acionamento", font=LARGE_FONT)
        label.pack(pady=10,padx=10)


     showAcionamento = tk.Button(self, text="0% de Iuminação", command = leD)
     showAcionamento.pack()

     sair = tk.Button(self, text="Sair", command = sairr)
     sair.pack()


app = showAcionam()
app.mainloop()


#Error Message
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
return self.func(*args)
File "/home/pi/TCC.py", line 34, in leD
showAcionamento["text"]="Lights off"
NameError: name 'showAcionamento' is not defined
1
  • 1
    alway add (in question) full error message. there is more usefull information ie. line which makes problem. Commented Oct 20, 2016 at 21:01

1 Answer 1

1

showAcionamento is local variable which exist only in __init__.
First you have to use self.showAcionamento in class to create object variable.

And now it wiil be available outside of classes as

app.frames[Acionamento].showAcionamento

so you will need

app.frames[Acionamento].showAcionamento["text"] = "Lights on"

and

app.frames[Acionamento].showAcionamento["text"] = "Lights off"
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Furas, I am struggling with subframes, how can I perform it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.