【问题标题】:How to pass id of a Tkinter Scale through command如何通过命令传递 Tkinter Scale 的 id
【发布时间】:2013-10-02 01:07:06
【问题描述】:

我正在使用 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 列表的索引来做到这一点。我需要知道正在调整哪个灯,以便我可以向相应的灯泡发送新的亮度。我希望我足够清楚。提前致谢!

【问题讨论】:

    标签: python-2.7 tkinter philips-hue


    【解决方案1】:

    我对您尝试对将回调函数分配给 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 小部件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 2016-07-25
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 2016-03-06
      相关资源
      最近更新 更多