button not named (tkinter with python3)

时间:2016-10-20 18:52:49

标签: python-3.x tkinter

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 个答案:

答案 0 :(得分:1)

showAcionamento是局部变量,仅存在于__init__中  首先,您必须在类中使用self.showAcionamento来创建对象变量。

现在它可以在课外使用

app.frames[Acionamento].showAcionamento

所以你需要

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

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