python - 在使用 tkinter Button 小部件在 python 中传递参数时需要帮助
问题描述
我正在读取一个 51 行 4 列的 csv 文件。我已将其存储在尺寸为 51 X 4 的 2D 列表中。我的目标是创建 51 个按钮,并且每次都将元素的值传递给由此创建的列表的 list[i][1] 作为其参数。但是,在这段代码中,list[50][1] 的值被传入所有 51 个按钮。如何将所需的不同 list[i][1] 值作为参数传递给每个第 i 个按钮?
这是我的代码。请找出其中的错误,非常感谢您的帮助。谢谢你。
file = open('stocklist.csv', 'r')
reader = csv.reader(file)
stocks = []
for line in reader:
w = line[0]
x = line[1]
y = line[2]
z = line[3]
stocks.append([w, x, y, z])
height = 51
width = 4
for i in range(height):
b = tk.Button(f, text=stocks[i][1], command=lambda: open_link(stocks[i][1]))
b.grid(row=i, column=1)
print(b)
解决方案
您可以使用 functool 的partial
(链接):
b = tk.Button(f, text=stocks[i][1], command=partial(open_link,stocks[i][1])
推荐阅读
- spring - Spring @Scheduled:如何检测方法是否被称为作业?
- python - Django 网页抓取
- python - 在另一个 ThreadPoolExecutor 中运行 ThreadPoolExecutor 是否安全?
- javascript - 离开 iPhone 上的 Chrome 选项卡时,Opentok stream.hasAudio 为假
- r - 在R中使用带有折叠的粘贴时如何创建引号
- python - 绘制多个模型成本
- javascript - 在 React 中显示复选框的值
- antlr4 - 至少一个 Antlr 规则可选部分
- javascript - 如何使用 JS/jQuery 通过 PHP 代理向 API 发送请求
- laravel - 使用 sanctum 的 SPA 身份验证失败,并显示以下错误消息:“未经身份验证”。