2016-10-28 10 views
-2

私はフレームをより多くのサブフレームに分割し、1つのサブフレームのボタンを呼び出そうとしていますが、私のコードでは私のサフフレームはフレームから離れています。誰かが私を助けることができたら、私は感謝します。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

私はそれを得て解決しました!フレームをサブフレームに分割するにはどうすればよいでしょうか?私はパックでそれをやろうとしていますが、メインフレームのタイトルはサブフレームから離れすぎています。いくつかの例を教えていただけますか? – armf1993

+0

あなたは 'padx'を使って、タイトルとフレームの間にマージンを作成します。タプルを使用して、左余白 'padx =(5、0)'のみを設定することができます。 BTW。サブフレームにウィジェット(ボタン)を1つだけ保持すると、サブフレームを作成する意味がありません。 – furas

+0

BTW。 'Label'にテキストを整列させることができます。画像を参照してください:https://github.com/furas/my-python-codes/tree/master/tkinter/grid-pack-align – furas

関連する問題