在python3中使用tkinter的子帧

时间:2016-10-28 08:42:04

标签: python python-3.x tkinter

我正在尝试将我的帧分成更多的子帧并为一个子帧调用一个按钮,但是在我的代码中,我的suframe似乎远离了我的帧。如果有人能帮助我,我将不胜感激。

#!/usr/bin/python

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

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)
        app.frames[Acionamento].vendasFrame.vendasAcionamento["text"]="Lights on"

    else:
        GPIO.output(5, GPIO.HIGH)
        GPIO.output(6, GPIO.HIGH)
        app.frames[Acionamento].vendasFrame.vendasAcionamento["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(padx=5,padx=5)

        #Subframe 1
        vendasFrame = tk.Frame(self)
        vendasFrame.pack(side=left, anchor="center")
        vendasAcionamento = tk.Button(vendasFrame, text="Luminárias desligadas", command = leD)
        vendasAcionamento.pack(side="top")
        self.vendasAcionamento = vendasAcionamento


        #Subframe 2
        engenhariaFrame = tk,Frame(self)
        engenhariaFrame.pack(side="left", anchor="center")


app = showAcionam()
app.mainloop()

错误:

    Exception in Tkinter callback
    Traceback (most recent call last):
       File "/home/pi/TCC.py", line 65, in leD
         app.frames[Acionamento].vendasFrame.vendasAcionamento["text"] = "100% de iluminação"
    AttributeError: 'Acionamento' object has no atribute 'vendasFrame'

1 个答案:

答案 0 :(得分:0)

您遇到与上一个问题相同的问题。

您必须使用this

self.

self.vendasFrame = tk.Frame(self) 是局部变量,仅存在于方法vendasFrame中。 __ini__是对象变量,您可以在此方法之外访问此变量。