我在获取某些按钮的ID时遇到问题

时间:2019-07-04 15:12:56

标签: python-3.x tkinter append

使用此应用程序,我稍后将尝试控制我公寓的灯。问题是我不知道如何获取按钮的ID。按钮处于循环中,我需要ID才能知道按下了哪个按钮。我得到的错误是:AttributeError:'_tkinter.tkapp'对象没有属性'buttonOn'

这是TKinter中的python应用程序,用于控制我公寓中的某些灯。

class Root(Tk):
 def __init__(self):
    super(Root, self).__init__()
    self.title('Lampensteuerung')
    self.geometry('1000x600')

    for l in lights:
        self.NameLamp = Label(self, text=lights[l].name)
        self.status= Label(self, text='Status:')
        self.statusLamp = Label(self, text=lights[l].on)
        self.brightness = Scale(self, from_=0, to=127, orient= HORIZONTAL)
        self.brightnessButton = Button(self, text='brightness control', width= 12, command = lambda: self.Brightness(c))
        self.buttonOn.append(Button(self, text='On', width=12, command = lambda c=l: self.LightOn(c)))
        self.buttonOff.append(Button(self, text='Off', width=12, command = lambda c=l: self.LightOff(c)))   

        self.NameLamp.grid(column=2, row=l, ipady=20, ipadx=20)
        self.status.grid(column=3, row=l, ipady=20, ipadx=20)
        self.statusLamp.grid(column=4, row=l, ipady=20, ipadx=20)
        self.brightness.grid(column=5, row=l, ipady=20, ipadx=20)
        self.brightnessButton.grid(column=6, row=l, ipady=20, ipadx=20)
        self.buttonOn.grid(column=7, row=l, ipady=20, ipadx=20)
        self.buttonOff.grid(column=8, row=l, ipady=20, ipadx=20)


def LightOn(self,c):
    if(c==1):
        lights[1].on = True
        self.statusLamp.config(text=lights[1].on)
    if(c==2):
        lights[2].on = True
        self.statusLamp.config(text=lights[2].on)
    if(c==3):
         lights[3].on = True
         self.statusLamp.config(text=lights[3].on)
    if(c==4):
        lights[4].on = True
        self.statusLamp.config(text=lights[4].on)
    if(c==5):
        lights[5].on = True
        self.statusLamp.config(text=lights[5].on)
    if(c==6):
        lights[6].on = True
        self.statusLamp.config(text=lights[6].on)
def LightOff(self,c):
    if(c==1):
        print("Licht 1 aus")

def Brightness(self,c):
    if(c==1):
        print("Helligkeit geändert")             
root = Root()
root.mainloop()

0 个答案:

没有答案