python - tkinter 中同一个按钮的多个实例
问题描述
我对编码很陌生,但我已经广泛搜索了这个并没有发现任何东西。我很可能遗漏了一些明显的东西,所以请多多包涵。我正在尝试编写一个简单的 tkinter 窗口,该窗口放置同一个按钮的多个实例,但是当我运行代码时,它只放置一次按钮。我可以制作几个按钮并分别对它们进行网格化,但是如果您想增加按钮的数量,这不能很好地扩展。这是我所拥有的:
from tkinter import *
root = Tk()
root.title("Button Grid")
def place_button(x, y):
button1.grid(row = y, column = x)
button1 = Button(root, text = "O", padx = 10, pady = 10, command = press)
plane = [
(0, 0), (1, 0), (2, 0),
(0, 1), (1, 1), (2, 1),
(0, 2), (1, 2), (2, 2)
]
for i in plane:
place_button(*i)
root.mainloop()
提前致谢。
解决方案
也许这可以帮助你...
from tkinter import *
root = Tk()
root.title("Button Grid")
root.geometry("500x700+100+50")
def place_button(x, y):
button1 = Button(root, text = "O", padx = 10, pady = 10, command = '')
button1.grid(row = y, column = x)
plane = [
(0, 0), (1, 0), (2, 0),
(0, 1), (1, 1), (2, 1),
(0, 2), (1, 2), (2, 2)
]
for i,j in plane:
place_button(i,j)
root.mainloop()
推荐阅读
- java - 在 Java 中编辑和更新 ArrayList
- php - 添加新列时如何合并所有产品表?
- excel - SpecFlow+ Excel 以编程方式生成特征文件
- javascript - 动画的帧重复?
- youtube - 如何阻止烦人的“登录 YouTube”弹出窗口?
- html - CSS HTML 列表项类
- google-pagespeed - 看到 Lighthouse 和 Page Speed Insights 之间的 30–40 点差异
- shodan - 您如何使用 Shodan 的 API 访问扫描结果?
- byte-buddy - 使用 Byte Buddy 拦截 Object.class toString() 方法
- python - 将lat lon字符串转换为geojson多边形