如何检索按钮的行和列信息并使用它来更改其在 python 中的设置
How to retrieve the row and column information of a button and use this to alter its settings in python
我正在创建游戏并尝试在 python 和 tkinter 中制作它。我已经用基于单词的 python 完成了它,并希望将其图形化。
我创建了一个按钮网格以用作网格,这些按钮当前带有字母 "O" 以显示空 space。但是,我想要的是显示海盗所在位置的文本,然后是玩家和箱子的按钮。由于位置是随机选择的,因此无法对文本进行硬编码。
while len(treasure)<chestLimit:
currentpos=[0,0]
T=[random.randrange(boardSize),random.randrange(boardSize)]
if T not in treasure or currentpos:
treasure.append(T)
while len(pirate)<pirateLimit:
P=[random.randrange(boardSize),random.randrange(boardSize)]
if P not in pirate or treasure or currentpos:
pirate.append(P)
boardselectcanv.destroy()
boardcanv=tkinter.Canvas(window, bg="lemonchiffon", highlightcolor="lemonchiffon", highlightbackground="lemonchiffon")
boardcreateloop=0
colnum=0
while colnum != boardSize:
rownum=0
while rownum != boardSize:
btn = tkinter.Button(boardcanv, text="O").grid(row=rownum,column=colnum)
boardcreateloop+=1
rownum+=1
colnum+=1
if btn(row,column) in pirate:
btn.configure(text="P")
boardcanv.pack()
这是创建网格的主要部分,一直到现在为止:
if btn(row,column) in pirate:
btn.configure(text="P")
所以我想知道是否有办法获取行和列并查看它是否在列表中?
谢谢
您可以在按钮小部件上调用 .grid_info()
以获取其网格信息。
from tkinter import *
root = Tk()
def showGrid():
row = btn.grid_info()['row'] # Row of the button
column = btn.grid_info()['column'] # grid_info will return dictionary with all grid elements (row, column, ipadx, ipday, sticky, rowspan and columnspan)
print("Grid position of 'btn': {} {}".format(row, column))
btn = Button(root, text = 'Click me!', command = showGrid)
btn.grid(row = 0, column = 0)
root.mainloop()
我正在创建游戏并尝试在 python 和 tkinter 中制作它。我已经用基于单词的 python 完成了它,并希望将其图形化。 我创建了一个按钮网格以用作网格,这些按钮当前带有字母 "O" 以显示空 space。但是,我想要的是显示海盗所在位置的文本,然后是玩家和箱子的按钮。由于位置是随机选择的,因此无法对文本进行硬编码。
while len(treasure)<chestLimit:
currentpos=[0,0]
T=[random.randrange(boardSize),random.randrange(boardSize)]
if T not in treasure or currentpos:
treasure.append(T)
while len(pirate)<pirateLimit:
P=[random.randrange(boardSize),random.randrange(boardSize)]
if P not in pirate or treasure or currentpos:
pirate.append(P)
boardselectcanv.destroy()
boardcanv=tkinter.Canvas(window, bg="lemonchiffon", highlightcolor="lemonchiffon", highlightbackground="lemonchiffon")
boardcreateloop=0
colnum=0
while colnum != boardSize:
rownum=0
while rownum != boardSize:
btn = tkinter.Button(boardcanv, text="O").grid(row=rownum,column=colnum)
boardcreateloop+=1
rownum+=1
colnum+=1
if btn(row,column) in pirate:
btn.configure(text="P")
boardcanv.pack()
这是创建网格的主要部分,一直到现在为止:
if btn(row,column) in pirate:
btn.configure(text="P")
所以我想知道是否有办法获取行和列并查看它是否在列表中?
谢谢
您可以在按钮小部件上调用 .grid_info()
以获取其网格信息。
from tkinter import *
root = Tk()
def showGrid():
row = btn.grid_info()['row'] # Row of the button
column = btn.grid_info()['column'] # grid_info will return dictionary with all grid elements (row, column, ipadx, ipday, sticky, rowspan and columnspan)
print("Grid position of 'btn': {} {}".format(row, column))
btn = Button(root, text = 'Click me!', command = showGrid)
btn.grid(row = 0, column = 0)
root.mainloop()