Python/ttk/tKinter - 使用按钮单击功能传递参数? [英] Python/ttk/tKinter - passing an argument with a button click func?
问题描述
我正在尝试将参数传递给按钮单击功能并遇到问题.
I'm trying to pass an argument to a button click func and encountering an issues.
简而言之,我试图按下按钮来弹出 askColor()
方法,并将该颜色值作为相关文本框的背景颜色返回.
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.
具体行:
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
.我尝试在按钮单击功能中包含 pack.forget
命令,但这也不起作用.
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.
推荐答案
你的问题的主要部分似乎是:
The main part of your question seems to be:
问题是我似乎无法像我一样引用 self.ABlob我正在尝试 - 它返回类型 None
The problem is that I can't seem to reference self.ABlob in the way I am trying - it returns type None
当你执行 x=ClassA(...).func(...)
时,x 包含调用 func
的结果.因此,当您执行 self.ABlob = ttk.Button(...).grid(...)
时,self.ABlob
中存储的是 无
,因为这是网格函数返回的内容.
When you do x=ClassA(...).func(...)
, x contains the result of the call to func
. Thus, when you do self.ABlob = ttk.Button(...).grid(...)
, what is stored in self.ABlob
is None
, because that is what is returned by the grid function.
如果要存储对按钮的引用,则需要创建按钮,然后以两个单独的步骤调用 grid:
If you want to store a reference to the button you will need to create the button and then call grid as two separate steps:
self.ABlob = ttk.Button(...)
self.ABlob.grid(...)
我个人认为这是最佳实践,尤其是当您使用网格时.通过将所有网格语句放在一个块中,可以更轻松地可视化布局和发现错误:
Personally I see this as a best practice, especially when you're using grid. By putting all of your grid statements in a block it becomes easier to visualize the layout and spot bugs:
self.ABlob.grid(row=3, column=2)
self.BBlob.grid(row=3, column=3)
self.CBlob.grid(row=3, column=4)
这篇关于Python/ttk/tKinter - 使用按钮单击功能传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!