tkinter - 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])
使用它,可以获得网格中按钮的行和列(这是我最终想要的,也许可以早点指定)。非常感谢在这里发表评论的两位最终为我指明了正确的方向。
解决方案
推荐阅读
- mongodb - mongodb 启动时自动关闭
- python - 如何使用箱线图检测和删除数据集中的异常值?
- bigcommerce - 使用隐身登录时 Facebook 页面插件无法正常工作
- c++ - 如何打印具有最低ascii和其他可能性的字符串
- google-fit - 用于计步的 Google Fit APi 授予第一次用户拒绝的权限
- python - 在列表列表中收集列表的第一项
- python - Odoo:如何在 product.category 中搜索 parent_id 及其所有子项
- python - 使用 numpy 和/或 cv2 在深度图中查找最暗区域
- sql - 这种 UNION 重复数据删除方案在 SQL 中如何工作?
- c++ - 使用 SetWindowPos 函数时的 MFC 调试断言