2013-01-13 44 views
1

我想通过一个参数传递给一个按钮,单击func并遇到问题。Python/ttk/tKinter - 用按钮单击func传递参数?

总之,我试图让按钮按下来弹出askColor()方法,并返回该颜色值作为相关文本框的背景颜色。

它的功能是如此synaesthets可以将一个颜色与一个字母/数字关联并记录结果的颜色列表。

具体线路:

self.boxA = Text(self.mainframe, state='normal', width=3, height=1, wrap='word', background=self.AVal).grid(column=2, row=2, padx=4) 
    self.boxB = Text(self.mainframe, state='normal', width=3, height=1, wrap='word', background=self.AVal).grid(column=3, row=2, padx=4) 
    self.boxC = Text(self.mainframe, state='normal', width=3, height=1, wrap='word', background=self.AVal).grid(column=4, row=2, padx=4) 

    self.ABlob = ttk.Button(self.mainframe, text="A",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxA)).grid(column=2, row=3) 
    self.BBlob = ttk.Button(self.mainframe, text="B",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxB)).grid(column=3, row=3) 
    self.CBlob = ttk.Button(self.mainframe, text="C",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxC)).grid(column=4, row=3) 

和:

def getColour(self,glyphRef): 
    (triple, hexstr) = askcolor() 
    if hexstr: 
      glyphRef.config(bg=hexstr) 

的问题是,我似乎不能在我想的方式来引用self.ABlob - 返回式None。我试过在button click func中包含一个pack.forget命令,但这也行不通。

回答

3

你的问题的主要部分似乎是:

的问题是,我似乎无法在我 正在尝试的方式引用self.ABlob - 返回式无

当你做x=ClassA(...).func(...)时,x包含调用func的结果。因此,当你做self.ABlob = ttk.Button(...).grid(...)时,self.ABlob中存储的内容是None,因为这是网格函数返回的内容。

如果你想存储到按钮的引用,您需要创建按钮,然后调用网格作为两个独立的步骤:

self.ABlob = ttk.Button(...) 
self.ABlob.grid(...) 

个人而言,我认为这是一个最好的做法,尤其是当你”重新使用网格。通过将所有网格语句放在一个块中,可以更容易地查看布局和现货缺陷:

self.ABlob.grid(row=3, column=2) 
self.BBlob.grid(row=3, column=3) 
self.CBlob.grid(row=3, column=4) 
+1

我认为你是对的,我正在吠叫错误的树。删除了我的答案。 – tacaswell

+0

啊!好。谢谢,我会看看这种方法,谢谢。 –

+0

工作就像一个魅力,我学到了一些关于'嵌套'命令。欣赏它。感谢您的时间。 –