1

I'm trying to pass an argument to a button click func and encountering an issues.

In short, I am trying to get a button press to pop up out the askColor() method, and return that colour value as the background colour of the related textbox.

Its function is so synaesthets can associate a colour with a letter/number and record the resulting colour list.

specific lines:

    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)

and:

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

The problem is that I can't seem to reference self.ABlob in the way I am trying - it returns type None. I have tried including a pack.forget command in the button click func, but that also doesn't work.

4

1 に答える 1

3

あなたの質問の主な部分は次のようです:

問題は、私がしようとしている方法で self.ABlob を参照できないように見えることです - タイプ None を返します

実行するとx=ClassA(...).func(...)、x には への呼び出しの結果が含まれますfunc。したがって、 を実行するとself.ABlob = ttk.Button(...).grid(...)、 に格納されるのself.ABlobは ですNone。これは、グリッド関数によって返されるものだからです。

ボタンへの参照を保存したい場合は、ボタンを作成してから grid を 2 つの別々のステップとして呼び出す必要があります。

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)
于 2013-01-13T13:43:04.847 に答える