-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'
好吧,我明白了,解决了它!你会推荐什么将帧分成子帧?我试图用pack来做到这一点,但主框架标题离子帧太远了。你能举个例子吗? – armf1993
您使用'padx',因此您可以在标题和框架之间创建边距。正如我记得你可以使用元组来设置左边距'padx =(5,0)'。 BTW。如果在子帧中只保留一个小部件(按钮),则无法创建子帧。 – furas
BTW。你可以在'Label'中对齐文本。看到图片:https://github.com/furas/my-python-codes/tree/master/tkinter/grid-pack-align – furas