如何通过命令传递Tkinter Scale的id

时间:2013-10-02 01:07:06

标签: python-2.7 tkinter philips-hue

我正在使用Tkinter为我正在编写的程序创建一个GUI,它将调整我拥有的一些Zigbee控制的LED灯。我正在使用循环创建一个Scale的多个副本,我将用作亮度滑块。我设法正确创建滑块,但我实际上很难正确调整滑块。这是我的代码:

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()

我意识到当您尝试运行它时,此代码可能会出错。基本上我的问题是,每个比例的命令只发送brightness_adj比例的值,但我不能让它通过灯的id。我试图通过发送创建时附加到的self.scale列表的索引来执行此操作。我需要知道哪个灯正在调整,以便我可以向相应的灯泡发送新的亮度。我希望我很清楚。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我对你要为scale小部件分配回调函数的行尝试做什么感到有点困惑:

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

因为self.vallist,并且您将其作为light_vallight_id参数发送,我认为这些参数应该是整数。

可能的解决方法:

我猜你希望每个回调都向brightness_adj函数发送一个不同的ID,具体取决于它所分配的比例。以下是我将如何解决这个问题:

将此函数添加到hueApp类命名空间:

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

然后将回调分配线从上面改为:

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

这应创建回调函数,将ID值保留在其内部命名空间中,并将它们分配给相应的scale小部件。