首页 文章

Python / tKinter / ttk - 在按钮点击时传递一个对象

提问于
浏览
1

TOP LEVEL EDIT:这个问题原则上已经在原始主题中得到了回答 - 但是,我还想了解一些基本问题 . 请不要直接回答这个问题 . 如果它的混乱,我很高兴删除这个Q!


与此问题相关我昨天问过:Python/ttk/tKinter - passing an argument with a button click func?

我正在尝试一种新的方法,但我可以(有点)让它工作,但是,只有当我“努力”编写函数时,我才会避免...

我有36个按钮[a-z]和[0-9] . 当用户单击该按钮时,会弹出 askcolor 对话框,我希望记录返回的颜色 .

在init类中,我声明了一些变量(都将颜色值设置为白色):

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.

当我填充文本框时,我使用color 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)

注意 - 我正在“发送”var的字符串,因为我无法正确地传递对象 - 理想情况下我只想发送对象,但这是我可以开始工作的唯一工作 .

然后我设置了按钮功能:

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个不同的按钮点击功能,但这似乎是反直觉的...尤其是当我对类功能的理解很少时,它就是为了应对这种挑战!

任何建议都感激不尽 .

1 回答

  • 1

    我最终将init值抛出到self.dict中,然后从click func更改dict .

    所以现在init是:

    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以供以后分析 . 任务完成 . :)

相关问题