python - Python Tkinter:如何在列表/元组中使用多个点创建_line?
问题描述
有一个坐标列表,我想在 Tkinter 画布上画线:
points = [(1,1), (2, 2), (3, 3), (2, 0)] # etc. This is could have 100 points or more
下一步将调用函数 create_line,但它不支持列表:
Canvas.create_line(1, 1, 2, 2, 3, 3) #this works ok
Canvas.create_line(points) #this doesn't work
那么有没有一种有效的方法来分离列表中的元素并按此顺序将它们插入到函数中?如果可能的话,我希望避免使用 for 循环。
解决方案
You can flatten the list points
with a list comprehension:
flattened = [a for x in points for a in x]
And turn the elements of that flattened list into arguments with the "*" syntax:
Canvas.create_line(*flattened)
I politely suggest that you overcome your hangup about for loops. It is pretty much impossible to write useful programs without them.
推荐阅读
- ztree - ztree 加载表:如何匹配变量
- google-cloud-platform - 如何使用 Dockerfile 将 Google 服务帐户密钥作为环境变量之一传递以在 Google Cloud Run 上部署
- php - 如何访问嵌套的 XML 节点?
- express - express-graphql - 如何将 res 对象传递给上下文中的自定义函数?
- python - 需要在没有特定端口的域名中运行 django 应用程序
- c - 为什么动态分配的数组不会随着新数据的到来而更新?
- c++ - 为什么在分配 r 值参考时被视为左值参考?
- java - 擦除方法与另一种方法相同
- php - 从 glob 数组 PHP 中排除多个键
- javascript - 如何简洁地使用 lodash 更新嵌套的对象数组?