2016-10-28 31 views
-2

我想將我的幀分成更多的子幀併爲一個子幀調用一個按鈕,但是我的代碼在下面,我的suframes似乎遠離我的框架。如果有人能幫助我,我將不勝感激。在python3中使用tkinter的子幀

#!/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' 

回答

0

你有同樣的問題在以前的問題。

你必須使用self.

self.vendasFrame = tk.Frame(self) 

vendasFrame是局部變量,只存在內部方法__ini__self.vendasFrame是對象變量,您可以在此方法之外訪問此變量。

+0

好吧,我明白了,解決了它!你會推薦什麼將幀分成子幀?我試圖用pack來做到這一點,但主框架標題離子幀太遠了。你能舉個例子嗎? – armf1993

+0

您使用'padx',因此您可以在標題和框架之間創建邊距。正如我記得你可以使用元組來設置左邊距'padx =(5,0)'。 BTW。如果在子幀中只保留一個小部件(按鈕),則無法創建子幀。 – furas

+0

BTW。你可以在'Label'中對齊文本。看到圖片:https://github.com/furas/my-python-codes/tree/master/tkinter/grid-pack-align – furas