tkinter中同一按钮的多个实例

问题描述 投票:0回答:1

我对编码还很陌生,但是我在Google上进行了广泛的搜索,但没有发现任何内容。我很可能会遗漏一些明显的东西,所以请多包涵。我正在尝试编写一个简单的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()

提前感谢。

python button tkinter grid
1个回答
0
投票

也许这可以帮助您...

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()
© www.soinside.com 2019 - 2024. All rights reserved.