首页 > 解决方案 > tkinter 抛出 TypeError,不确定它引用的是什么

问题描述

我正在创建一个按钮网格,然后在使用 tkinter 单击按钮时尝试查找按钮的位置。但是,TypeError运行代码时 tkinter 会抛出 a ,我不确定它引用的是哪一行。我相信有问题的区域如下所示,但如果需要,我可以发布我写的所有内容。

def createbuttongrid(self):
    label = 1
    for row in range(8):
        for column in range(12):

            button = tk.Button(self.button_frame, text='Well %s' % label, command=lambda r=row, c=column:self.colorbuttons)
            button.bind("<ButtonPress-1>", self.colorbuttons)
            self.button_list[button] = (row, column)
            button.grid(row=row, column=column, sticky='nsew')
            label += 1

def colorbuttons(self, r, c):
    row = r
    column = c
    print("Row: %s\nColumn: %s" % (row, column))

我得到的错误如下

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\[MY_NAME]\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
TypeError: colorbuttons() missing 1 required positional argument: 'c'

这两个函数都在一个类中,包括__init__生成和设置主窗口(如果重要)。

谢谢你的帮助!

编辑

我最终在下游改变了一些东西,并得到了以下结果

def createbuttongrid(self):
    label = 1
    for row in range(8):
        for column in range(12):

            button = tk.Button(self.button_frame, text='Well %s' % label)
            button.bind("<Button-1>", self.colorbuttons)
            button.grid(row=row, column=column, sticky='nsew')
            self.button_list[button] = (row, column)
            label += 1

def colorbuttons(self, event):
    button = event.widget
    print(button, self.button_list[button])

使用它,可以获得网格中按钮的行和列(这是我最终想要的,也许可以早点指定)。非常感谢在这里发表评论的两位最终为我指明了正确的方向。

标签: tkintertypeerrorpython-3.7

解决方案


推荐阅读