Search code examples
python-2.7tkinterphilips-hue

How to pass id of a Tkinter Scale through command


I am using Tkinter to create a GUI for a program I am writing that will adjust some Zigbee controlled LED lights that I have. I am using a loop to create multiple copies of a Scale that I'm going to use as a brightness slider. I manage to create the sliders properly, but I am having difficulties actually adjust the sliders correctly. Here's my code:

import simplejson as json  
import requests         # submits http requests
from Tkinter import *
from ttk import Frame, Button, Label, Style, Notebook

# MD5 hash from  http://www.miraclesalad.com/webtools/md5.php
myhash = "d9ffaca46d5990ec39501bcdf22ee7a1"
appname = "dddd"    # name content isnt relevant
num_lights = int(3)

class hueApp(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()      

    def initUI(self, *args, **kwds):

        # title the app window
        self.parent.title("Hue controller")
        self.style = Style() 

        # create grid layout
        self.columnconfigure(0, pad=3)
        self.columnconfigure(1, pad=3)
        self.columnconfigure(2, pad=3)

        self.rowconfigure(0, pad=3)

        self.scale=[]
        self.val=[]
        for i in range(num_lights):
            print 'i=', i, type(i)
            self.val.append(i+1)
            print 'val=', self.val, type(self.val)
            self.scale.append(Scale(self, from_=255, to_=0, command=lambda i=self.val: self.brightness_adj(i,light_id=i)))
            print self.scale[i]
            print 'i = ', i, type(i), '\n\n'
            self.scale[i].set(150)
            self.scale[i].grid(row=1, column=i)

            if i == 2:
                print '\n', self.scale, '\n'
                print self.val, '\n'
                self.scale[i].set(200)



        self.centerWindow
        self.pack()

    def brightness_adj(self,light_val, light_id):
        #global bri_val
        print 'light_id:', light_id, type(light_id)
        print 'light_val:', light_val, type(light_val)

        print self.val[int(light_id)]
        #print int(light_id)
        bri_val = self.scale[light_id-1].get()
        print bri_val

        light = light_id
        global huehub
        huehub = "http://192.168.0.100/api/"+ myhash + "/lights/" + str(light)
        #brightness_logic()
        reply = requests.get(huehub)
        a=json.loads(reply.text)
        #print bri_val
        payload = json.dumps({"bri":bri_val})
        sethuehub = huehub + "/state"
        reply = requests.put(sethuehub, data=payload)

    def centerWindow(self):

        w = 250
        h = 150

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw-w)/2
        y = (sh-h)/2
        self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))

def main():
    root=Tk() #the root window is created
    app=hueApp(root) #create an instance of the application class

    root.mainloop()  

if __name__ == '__main__':
        main()

I realize that this code probably gives an error when you try to run it. Basically my problem is that the command for each scale is only send brightness_adj the value of the scale, but I can't get it to pass through the id of the light. I was trying to do this by sending through the index of the self.scale list that it is appended into when it is created. I need to know which light is being adjusted so that I can send a new brightness to the corresponding light bulb. I hope I was clear enough. Thanks in advance!


Solution

  • I'm a little confused about what you're trying to do with the line that assigns callback functions to the scale widgets:

    self.scale.append(Scale(self, from_=255, to_=0, command=lambda i=self.val: self.brightness_adj(i,light_id=i)))
    

    since self.val is a list, and you're sending it as both the light_val and the light_id arguments, which I would think should be integers.

    Possible fix:

    I'm guessing that you want each callback to send a different ID to the brightness_adj function depending on which scale it's assigned to. Here's how I would fix this up:

    Add this function to your hueApp class namespace:

    def brightnessCallbackFactory(self, id):
        return lambda light_val:self.brightness_adj(light_val, id)
    

    Then change the callback assignment line from the above to this:

    self.scale.append(Scale(self, from_=255, to_=0, command=self.brightnessCallbackFactory(i)))
    

    That should create callback functions that retain the ID value in their internal namespace and assign them to the corresponding scale widget.