首页 > 解决方案 > 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()

提前致谢。

标签: pythonbuttontkintergrid

解决方案


也许这可以帮助你...

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()

推荐阅读