Tkinter:按行和列识别按钮
Tkinter: Identifying button by row and column
我希望能够 select 根据按钮在网格中的行和列以及按钮来控制其文本和浮雕。我无法在以这种方式使用的小部件或单元格上找到任何内容。
编辑:
我更改了 root 的放置位置,现在它说我不能使用我为 'relief' 收到的元组,这是有道理的,我需要访问小部件本身。任何推荐
import tkinter
import functools
import random
from time import sleep
width = input('Enter the grid width. ')
height = input('Enter the grid height. ')
numb = input('Enter the number of bombs. ')
Matrix = [[0 for lp in range(int(width))] for fg in range(int(height))]
def ranintx():
return random.randint(0,int(width))
def raninty():
return random.randint(0,int(height))
def placemines():
y = ranintx()
x = raninty()
for ranintformine in range(int(numb)):
x = ranintx()
y = raninty()
Matrix[y-1][x-1] = 1
placemines()
def sunken(event, self, x, y):
button = event.widget
button['relief'] = 'sunken'
if x - 1 < 0 :
return
if x > int(width) + 1 :
return
if y - 1 < 0 :
return
if y > int(height) + 1 :
return
if Matrix[x][y] == 1 :
top = tkinter.Toplevel()
top.title("About this application...")
msg = tkinter.Message(top, text="You Lose")
msg.pack()
button = tkinter.Button(top, text="Dismiss", command=top.destroy)
button.pack()
print('Column = {}\nRow = {}'.format(x, y))
else:
n1 = x - 1
n2 = y - 1
for lp in range(3):
for lp2 in range(3):
abutton = root.grid_location(n1, n2)
abutton['relief'] = ['sunken']
# I want to be able to change and select the button here. This was one of my poor attempt
n2 =+ 1
n1 =+ 1
def push(event, self, x, y):
button = event.widget
if Matrix[x][y] == 1 :
print('Column = {}\nRow = {}'.format(x, y))
class MineSweep(tkinter.Frame):
@classmethod
def main(cls, width, height):
window = cls(root, width, height)
'''placemine()'''
root.mainloop()
def __init__(self, master, width, height):
super().__init__(master)
self.__width = width
self.__height = height
self.__build_buttons()
self.grid()
#def sunken(event):
# button = event.widget
# button['relief'] = 'sunken'
def __build_buttons(self):
self.__buttons = []
for y in range(self.__height):
row = []
for x in range(self.__width):
button = tkinter.Button(self, state='disabled')
button.grid(column=x, row=y)
button['text'] = ' '
print(grid.slaves)
self.checked = True
#button['command'] = functools.partial(self.__push, x, y)
button.bind("<Button-3>",
lambda event, arg=x, brg=y: push(event, self, arg, brg))
button['relief'] = 'raised'
button.bind("<Button-1>",
lambda event, arg=x, brg=y: sunken(event, self, arg, brg))
#button['command'] = sunken
row.append(button)
self.__buttons.append(row)
root = tkinter.Tk()
if __name__ == '__main__':
MineSweep.main(int(width), int(height))
您的程序有一些问题。首先,sunken
应该是 class 上的一个方法。将它放在 class 之外是非常奇怪的,然后你将 self
作为其他参数传入。它有效,但它使代码非常混乱。
话虽如此,您实际上已经非常接近完成这项工作了。您已经在列表列表中保存了对每个按钮的引用,因此您应该能够使用 self.__buttons[y][x]
获取小部件。 但是,因为sunken
不是class的一部分,而且因为你用两个下划线命名变量,[=10=不能访问变量] 函数。
如果您将变量更改为具有单个下划线而不是双下划线,您的代码应该或多或少完全按原样工作(一旦您修复了语法和缩进错误)。另一个解决方案是使 sunken
成为 class 上的一个方法并修复你如何调用它(删除 self 参数,将其称为 self.sunken
),它将使用两个下划线。
坦率地说,使用两个下划线的实际好处为零。避免使用它的诱惑。至少,在你的基本逻辑工作之前不要使用它,然后你可以返回并隐藏你不想暴露的属性。
我希望能够 select 根据按钮在网格中的行和列以及按钮来控制其文本和浮雕。我无法在以这种方式使用的小部件或单元格上找到任何内容。 编辑: 我更改了 root 的放置位置,现在它说我不能使用我为 'relief' 收到的元组,这是有道理的,我需要访问小部件本身。任何推荐
import tkinter
import functools
import random
from time import sleep
width = input('Enter the grid width. ')
height = input('Enter the grid height. ')
numb = input('Enter the number of bombs. ')
Matrix = [[0 for lp in range(int(width))] for fg in range(int(height))]
def ranintx():
return random.randint(0,int(width))
def raninty():
return random.randint(0,int(height))
def placemines():
y = ranintx()
x = raninty()
for ranintformine in range(int(numb)):
x = ranintx()
y = raninty()
Matrix[y-1][x-1] = 1
placemines()
def sunken(event, self, x, y):
button = event.widget
button['relief'] = 'sunken'
if x - 1 < 0 :
return
if x > int(width) + 1 :
return
if y - 1 < 0 :
return
if y > int(height) + 1 :
return
if Matrix[x][y] == 1 :
top = tkinter.Toplevel()
top.title("About this application...")
msg = tkinter.Message(top, text="You Lose")
msg.pack()
button = tkinter.Button(top, text="Dismiss", command=top.destroy)
button.pack()
print('Column = {}\nRow = {}'.format(x, y))
else:
n1 = x - 1
n2 = y - 1
for lp in range(3):
for lp2 in range(3):
abutton = root.grid_location(n1, n2)
abutton['relief'] = ['sunken']
# I want to be able to change and select the button here. This was one of my poor attempt
n2 =+ 1
n1 =+ 1
def push(event, self, x, y):
button = event.widget
if Matrix[x][y] == 1 :
print('Column = {}\nRow = {}'.format(x, y))
class MineSweep(tkinter.Frame):
@classmethod
def main(cls, width, height):
window = cls(root, width, height)
'''placemine()'''
root.mainloop()
def __init__(self, master, width, height):
super().__init__(master)
self.__width = width
self.__height = height
self.__build_buttons()
self.grid()
#def sunken(event):
# button = event.widget
# button['relief'] = 'sunken'
def __build_buttons(self):
self.__buttons = []
for y in range(self.__height):
row = []
for x in range(self.__width):
button = tkinter.Button(self, state='disabled')
button.grid(column=x, row=y)
button['text'] = ' '
print(grid.slaves)
self.checked = True
#button['command'] = functools.partial(self.__push, x, y)
button.bind("<Button-3>",
lambda event, arg=x, brg=y: push(event, self, arg, brg))
button['relief'] = 'raised'
button.bind("<Button-1>",
lambda event, arg=x, brg=y: sunken(event, self, arg, brg))
#button['command'] = sunken
row.append(button)
self.__buttons.append(row)
root = tkinter.Tk()
if __name__ == '__main__':
MineSweep.main(int(width), int(height))
您的程序有一些问题。首先,sunken
应该是 class 上的一个方法。将它放在 class 之外是非常奇怪的,然后你将 self
作为其他参数传入。它有效,但它使代码非常混乱。
话虽如此,您实际上已经非常接近完成这项工作了。您已经在列表列表中保存了对每个按钮的引用,因此您应该能够使用 self.__buttons[y][x]
获取小部件。 但是,因为sunken
不是class的一部分,而且因为你用两个下划线命名变量,[=10=不能访问变量] 函数。
如果您将变量更改为具有单个下划线而不是双下划线,您的代码应该或多或少完全按原样工作(一旦您修复了语法和缩进错误)。另一个解决方案是使 sunken
成为 class 上的一个方法并修复你如何调用它(删除 self 参数,将其称为 self.sunken
),它将使用两个下划线。
坦率地说,使用两个下划线的实际好处为零。避免使用它的诱惑。至少,在你的基本逻辑工作之前不要使用它,然后你可以返回并隐藏你不想暴露的属性。