![](/img/trans.png)
[英]Python/ttk/tKinter - passing an argument with a button click func?
[英]Python/tKinter/ttk - passing an object on button click
最高级别的编辑:这个问题原则上已经在原始主题中回答-但是,我想了解更多有关一些基本问题。 请不要直截了当地回答问题。 如果这个问题比较混乱,我很高兴将其删除!
与我昨天问过的这个问题有关: Python / ttk / tKinter-用按钮单击函数传递参数?
我正在尝试一种新方法,但是(只要)我对功能进行了“硬”编码,就可以(一定)使它起作用,而我试图避免这种情况...
我有36个按钮[az]和[0-9]。 当用户单击按钮时, askcolor
对话框会弹出,我希望记录返回的颜色。
在类初始化中,我声明了一些变量(都将颜色值设置为白色):
class MakeGUI(Frame):
def __init__(self,root):
Frame.__init__(self, root)
self.root = root
##colour values
self.AVal = "#FFFFFF"
self.BVal = "#FFFFFF"
self.CVal = "#FFFFFF"
etc.
当我填充文本框时,我将var用作bg arg:
## letter labels
self.boxA = Text(self.mainframe, state='normal', width=3, height=1, background=self.AVal).grid(column=2, row=2, padx=4)
self.boxB = Text(self.mainframe, state='normal', width=3, height=1, background=self.BVal).grid(column=3, row=2, padx=4)
self.boxC = Text(self.mainframe, state='normal', width=3, height=1, background=self.CVal).grid(column=4, row=2, padx=4)
etc.
然后我设置按钮:
self.bloba = ttk.Button(self.mainframe, text="A",style= 'mainSmall.TButton', command= lambda: self.getColour("self.AVal")).grid(column=2, row=3)
self.blobb = ttk.Button(self.mainframe, text="B",style= 'mainSmall.TButton', command= lambda: self.getColour("self.BVal")).grid(column=3, row=3)
self.blobc = ttk.Button(self.mainframe, text="C",style= 'mainSmall.TButton', command= lambda: self.getColour(("self.CVal")).grid(column=4, row=3)
注意-我正在“发送”变量的字符串,因为我无法正确地传递对象-理想情况下,我只想发送对象,但这是我唯一可以解决的方法。
然后设置按钮func:
def getColour(self,glyphRef):
print self.AVal
(triple, hexstr) = askcolor()
if hexstr:
eval(glyphRef+"=hexstr")
print self.AVal
仅在我弄清楚如何使其工作时,这两个打印语句才用于监视目的。
我也很清楚eval
方法不是理想的还是理想的。 我只是找不到让我接近理想解决方案的替代方法。
此方法引发错误:
#FFFFFF
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "colourPicker/colourTest.py", line 109, in <lambda>
self.bloba = ttk.Button(self.mainframe, text="A",style= 'mainSmall.TButton', command= lambda: self.getColour("self.AVal")).grid(column=2, row=3)
File "colourPicker/colourTest.py", line 161, in getColour
eval(glyphRef+"=hexstr")
File "<string>", line 1
self.AVal=hexstr
^
SyntaxError: invalid syntax
为了尝试测试发生了什么,我尝试在按钮func中对var进行硬编码:
def getColour(self,glyphRef):
print self.AVal
(triple, hexstr) = askcolor()
if hexstr:
self.AVal=hexstr
print self.AVal
这确实导致var被更改:
#FFFFFF
#24d9d9
这意味着我可以编写36种不同的按钮单击功能,但这似乎很直观……尤其是当我对类功能知之甚少时,它就可以应对这种挑战!
任何建议表示感谢。
我最终将init值放入self.dict,然后从click func更改了dict。
所以现在的初始化是:
self.colourDict = \
{"AVal":"#FFFFFF",
"BVal":"#FFFFFF",
"CVal":"#FFFFFF",
"DVal":"#FFFFFF",
etc.
文本框制作器为:
self.boxA = Text(self.mainframe, state='normal', width=3, height=1, background=self.colourDict['AVal'])
self.boxB = Text(self.mainframe, state='normal', width=3, height=1, background=self.colourDict['BVal'])
self.boxC = Text(self.mainframe, state='normal', width=3, height=1, background=self.colourDict['CVal'])
etc.
然后将其网格化:
self.boxA.grid(column=2, row=2, padx=4)
self.boxB.grid(column=3, row=2, padx=4)
self.boxC.grid(column=4, row=2, padx=4)
etc.
然后是按钮:
self.bloba = ttk.Button(self.mainframe, text="A",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxA,"AVal")).grid(column=2, row=3)
self.blobb = ttk.Button(self.mainframe, text="B",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxB,"BVal")).grid(column=3, row=3)
self.blobc = ttk.Button(self.mainframe, text="C",style= 'mainSmall.TButton', command= lambda: self.getColour(self.boxC,"CVal")).grid(column=4, row=3)
etc.
指向按钮功能的哪一点:
def getColour(self,glyphRef, value):
(triple, hexstr) = askcolor()
if hexstr:
glyphRef.config(bg=hexstr)
self.colourDict[value] = hexstr
这既设置了视觉颜色,又给了我一个可调用的命令,可以用来将结果保存为XML以供以后分析。 任务完成。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.